I'm currently dealing with handlebars.js in an express.js application. To keep things modular, I split all my templates in partials.
My problem: I couldn't find a way to pass variables through an partial invocation. Let's say I have a partial which looks like this:
<div id=myPartial>
<h1>Headline<h1>
<p>Lorem ipsum</p>
</div>
Let's assume I registered this partial with the name 'myPartial'. In another template I can then say something like:
<section>
{{> myPartial}}
</section>
This works fine, the partial will be rendered as expected and I'm a happy developer. But what I now need, is a way to pass different variables throught this invocation, to check within a partial for example, if a headline is given or not. Something like:
<div id=myPartial>
{{#if headline}}
<h1>{{headline}}</h1>
{{/if}}
<p>Lorem Ipsum</p>
</div>
And the invokation should look something like this:
<section>
{{> myPartial|'headline':'Headline'}}
</section>
or so.
I know, that I'm able to define all the data I need, before I render a template. But I need a way to do it like just explained. Is there a possible way?
Yes, I was late, but I can add for Assemble users: you can use buil-in
"parseJSON"
helper http://assemble.io/helpers/helpers-data.html. (Discovered in https://github.com/assemble/assemble/issues/416).This can also be done in later versions of handlebars using the
key=value
notation:Allowing you to pass specific values to your partial context.
Reference: Context different for partial #182
Sounds like you want to do something like this:
Yehuda already gave you a way of doing that:
But to clarify:
To give your partial its own data, just give it its own model inside the existing model, like so:
In other words, if this is the model you're giving to your template:
Then add a new object to be given to the partial:
childContext
becomes the context of the partial like Yehuda said -- in that, it only sees the fieldanother
, but it doesn't see (or care about the fieldsome
). If you hadid
in the top level model, and repeatid
again in the childContext, that'll work just fine as the partial only sees what's insidechildContext
.Handlebars partials take a second parameter which becomes the context for the partial:
In versions v2.0.0 alpha and later, you can also pass a hash of named parameters:
You can see the tests for these scenarios: https://github.com/wycats/handlebars.js/blob/ce74c36118ffed1779889d97e6a2a1028ae61510/spec/qunit_spec.js#L456-L462 https://github.com/wycats/handlebars.js/blob/e290ec24f131f89ddf2c6aeb707a4884d41c3c6d/spec/partials.js#L26-L32
Just in case, here is what I did to get partial arguments, kind of. I’ve created a little helper that takes a partial name and a hash of parameters that will be passed to the partial:
The key thing here is that Handlebars helpers accept a Ruby-like hash of arguments. In the helper code they come as part of the function’s last argument—
options
— in itshash
member. This way you can receive the first argument—the partial name—and get the data after that.Then, you probably want to return a
Handlebars.SafeString
from the helper or use “triple‑stash”—{{{
— to prevent it from double escaping.Here is a more or less complete usage scenario:
Hope this helps …someone. :)
The accepted answer works great if you just want to use a different context in your partial. However, it doesn't let you reference any of the parent context. To pass in multiple arguments, you need to write your own helper. Here's a working helper for Handlebars
2.0.0
(the other answer works for versions<2.0.0
):Then in your template, you can do something like:
And in your partial, you'll be able to access those values as context like: