Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to root context in Handlebar.js template

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.

like image 326
isHristov Avatar asked Jun 28 '13 12:06

isHristov


2 Answers

Use @root. This is in handlebars-v2.0.0.js

{{@root.somthing.nested_somthing}}
like image 178
jaegow Avatar answered Oct 19 '22 16:10

jaegow


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

like image 39
equivalent8 Avatar answered Oct 19 '22 15:10

equivalent8