Is there any built-in method for accessing the root context in a Handlebars.js template? Most of the helpers are adding a nested context and you have to write ../ before the variable in that context to access it but that is not very practical if you have lots of eachs, ifs, etc.
Use @root. This is in handlebars-v2.0.0.js
{{@root.somthing.nested_somthing}}
there is no possibility to access root context of template once you change the context with looping (e.g. each) more info
However there is a possibility to access previous context with '../'
# app/assets/javascript/contents.coffee
body = HandlebarsTemplates['my_hbs_template']({
view:{
registryName: 'foo',
data: {items: {x: 'x'}}
}
})
template:
<!-- app/assets/javascript/templates/my_content.hbs -->
<table class="table">
<tbody>
{{#each view.data.items}}
<tr>
<td>{{@key}}</td>
<td>
Hello from {{../view.registryName}}
</td>
</tr>
{{/each}}
</tbody>
</table>
check http://handlebarsjs.com/#paths for more info
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With