Basic DocPad: variables and first errors

110 Views Asked by At

What I don't understand: certain keywords appear to be reserved for DocPad like @document, which words let me use custom data values?

e.g. I'm using

<%= data.hostimagesurl %>

but I've seen

<%= page.hostimagesurl %>

and

<%= site.hostimagesurl %> 

also in use, can I make these up? Are there certain values I have to use?

I don't understand where I can discover the pitfalls, are hyphens and underscores allowed?

I'm imagining this works like handlebars, I define the tag enter the value and it just works - is this way of thinking correct?

I'm also confused to why my DocPad layout isn't working. I'm just getting an error

error: Something went wrong while rendering: /Users/***/my-new-website/src/render/index.html
The error follows:

ReferenceError: document is not defined
  at Object.eval (<anonymous>:55:29)
  at Object.eval (<anonymous>:67:8)
  at eval (<anonymous>:69:6)
  at Function.eco.render (/Users/***/my-new-website/node_modules/eco/lib/index.js:26:25)
  at EcoPlugin.render (/Users/***/my-new-website/node_modules/docpad-plugin-eco/out/eco.plugin.js:23:32)
  at ambi (/Users/***/my-new-website/node_modules/event-emitter-grouped/node_modules/ambi/out/lib/ambi.js:57:27)
  at Task.<anonymous> (/Users/***/my-new-website/node_modules/event-emitter-grouped/out/lib/event-emitter-grouped.js:45:23)
  at ambi (/Users/***/my-new-website/node_modules/ambi/es5/lib/ambi.js:98:14)
  at Domain.fireMethod (/Users/***/my-new-website/node_modules/taskgroup/out/lib/taskgroup.js:397:23)
  at Domain.run (domain.js:228:14)
  at Task.fire (/Users/***/my-new-website/node_modules/taskgroup/out/lib/taskgroup.js:435:27)
  at Immediate._onImmediate (/Users/***/my-new-website/node_modules/taskgroup/out/lib/taskgroup.js:452:26)
  at processImmediate [as _immediateCallback] (timers.js:383:17)

The first error I can see happens on this line:

background-image:url(<%= data.hostimagesurl %>bg.gif);

<body style="padding-top:0;padding-bottom:0;padding-right:0;padding-left:0;min-width:100%; color:#353535; background-color:#f9f9f9; background-image:url(<%= data.hostimagesurl %>bg.gif); background-repeat: repeat; background-position:center top; font-family: Helvetica, sans-serif; font-size:13px; margin: 0; padding: 0;" yahoo="fix" bgcolor="#f9f9f9">

My render index.html file looks like:

---
title: "Welcome!"
layout: "default"
isPage: true

hostimagesurl: "http://www.googel.com/" 

---

<p>Testing 1</p>

What on earth am I doing wrong?

2

There are 2 best solutions below

0
On

In your example you've made the classic DocPad error of forgetting the @ symbol when referring to the current document: @document is defined but document isn't.

Likewise, to access hostimagesurl you would need to call @document.hostimagesurl

By default two objects are passed to a DocPad template/page/layout. The templateData property defined in the docpad.coffee file and the current document object. These objects are properties of the template this context. In the case of templateData each of its properties are a member of the this context, whereas the document itself is a property of this.

In CoffeeScript and the CoffeeScript based template system, ECO, this is referred to by the @ symbol.

What this means is that within an ECO template you access the current document with @document. You can also access @site.url or @getPreparedTitle() which are defined in the docpad.coffee file as part of the templateData.

The properties defined in the metadata section of your doc (between the ---) are available as properties of the document object.

Quite often people will create variables like page when looping through a collection. These local variables don't need the this context. Typically something like this:

<%pages = @getCollection('pages').toJSON()%>
<ul>
    <%for page in pages:%>
        <li>
            <a href="<%-page.hostimagesurl%>"><%-page.title%></a>
        </li>
    <%end%>
</ul>
0
On
@document

is just CoffeeScript (and eco) special syntax for

this.document

The variable site can be defined in your docpad.coffee file, see http://docpad.org/docs/begin and search for docpadConfig

I am still learning and cannot tell you about your page.whatever code but they are often used inside of a loop where the variable is named in the for loop code.