Question

Although something like this works in assemble.io pages:

---
title: "My title"
woo: <%= title %>
---

(i.e. {{woo}} outputs 'My title' to the page), if I have the same thing in a partial there's nothing in 'woo'. Does anyone know if this is by design, or am I doing something wrong?

Thanks!

Was it helpful?

Solution

In Assemble the convention is to use the YAML front matter as the value of "this", and context for partials should be namespaced using the name of the partial as the first path in the context.

So in a page, you can do as you noted, but with a partial named alert.hbs, you would have to do something like {{alert.title}}, or:

{{#alert}}
  {{title}}
{{/alert}}

We know this is can be a little confusing, and IMO a little frustrating since YAML front matter doesn't work the same way in partials as pages, but until this Handlebars issue is resolved we have to live with the limitations of that library: https://github.com/wycats/handlebars.js/issues/392 (Assemble now supports Swig Templates as well, but that doesn't solve this problem for you).

Last, you can create custom helpers to get around it. The assemble-yaml lib has methods for accessing YAML front matter in templates (the yfm project shows some alternatives and examples for how you can use context from YFM in templates), and their is a grunt-init template for creating Handlebars helpers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top