docpad-archive / extras

A test runner for all of DocPad's officially supported extensions
https://docpad.org/docs/plugins
22 stars 26 forks source link

How to access data from docpad.cson in partials? #30

Closed zenorocha closed 11 years ago

zenorocha commented 11 years ago

I'm trying to use a variable from docpad.cson in my src/partial/header.html.eco by typing <%= @conf.name %>, but I receive this error:

warning: Something went wrong while rendering: /Users/zeno/Projects/braziljs/conf-boilerplate-docpad/src/partials/header.html.eco
warning: Rendering partial failed: header.html.eco. The error follows:
warning: An error occured: Cannot read property 'conf' of undefined TypeError: Cannot read property 'conf' of undefined

This does not happen when I use this variable in my src/layouts/default.html.eco for example.

So, can I access data from docpad.cson in partials @balupton?

balupton commented 11 years ago

Sure. So two things are important here:

  1. Templates only ever have access to template data, so inside your docpad configuration file, anything inside templateData your templates will have access to.
  2. Partials do not inherit the template data normally, as this would be quite slow, and in most cases unnecessary. However, you can pass a partial the template data, by doing this <%- @partial('the-partial', @) %> - which says, render the-partial and send it everything I currently have access to (@ is this in coffeescript)

So, you'll want to:

  1. Make sure whatever you want to render from your configuration file is accessible via the templateData configuration variable.
  2. Send @ with your partial call. If you need to send @ and some other special data, you can do <%- @partial('the-partial', @, {one:1,two:2,etc:3}) %>

Let me know how it goes :)

zenorocha commented 11 years ago

Hey @balupton,

This line of code <%- @partial('the-partial', @) %> saved my day.

Thanks again for your awesome support!

I'm working in two important projects that uses DocPad, one for BrazilJS Foundation and another for Liferay. And your help has been precious.