Access a variable outside the scope of a Handlebars.js each loop
Asked Answered
C

4

229

I have a handlebars.js template, just like this:

{{externalValue}}

<select name="test">
    {{#each myCollection}}
       <option value="{{id}}">{{title}} {{externalValue}}</option>
    {{/each}}
</select>

And this is the generated output:

myExternalValue

<select name="test">
       <option value="1">First element </option>
       <option value="2">Second element </option>
       <option value="3">Third element </option>
</select>

As expected, I can access the id and title fields of every element of myCollection to generate my select. And outside the select, my externalValue variable is correctly printed ("myExternalValue").

Unfortunately, in options' texts, externalValue value is never printed out.

My question is: how can I access a variable outside the scope of the handlebars.js each from within the loop?

Commit answered 30/11, 2012 at 12:17 Comment(0)
M
547

Try

<option value="{{id}}">{{title}} {{../externalValue}}</option>

The ../ path segment references the parent template scope that should be what you want.

Molybdenum answered 30/11, 2012 at 12:30 Comment(6)
If future readers are still having trouble like I was, have a look at the comment for this answer here. It took me a while after seeing this answer to see that one. You may need to use ../ repeatedly depending on how many scopes away from the value you are.Basilisk
Am I crazy or is this type of valuable information nowhere to be found on the handlebars docs???Zoes
@Molybdenum Ugh, yeah, I'm an idiot. I don't know how I didn't see that. Forest for the trees or something.Zoes
@Molybdenum will this work on ember handlebars.. it seems not to be workingCnidoblast
No idea @kweku360. This works for normal hadlebars. Might be that Ember is using customized version of handlebars where this functionality is implemented with another meansMolybdenum
Thank you man, this works perfectly also with Foundation 6 Panini.Gaskell
B
19

Or you can use absolute path like this:

<option value="{{id}}">{{title}} {{@root.user.path.to.externalValue}}</option>
Brolly answered 16/11, 2018 at 6:19 Comment(0)
W
9

I saw many links with 404 for documentation about this topic.

I update it with this one, it is working in April 1st 2020:

https://handlebarsjs.com/guide/expressions.html#path-expressions

Some helpers like #with and #each allow you to dive into nested objects. When you include ../ segments into your path, Handlebars will change back into the parent context.

    {{#each people}}
    {{../prefix}} {{firstname}} 
    {{/each}}

Even though the name is printed while in the context of a comment, it can still go back to the main context (the root-object) to retrieve the prefix.

WARNING

The exact value that ../ will resolve to varies based on the helper that is calling the block. Using ../ is only necessary when context changes. Children of helpers such as {{#each}} would require the use of ../ while children of helpers such as {{#if}} do not.

{{permalink}}
{{#each comments}}
  {{../permalink}}

  {{#if title}}
    {{../permalink}}
  {{/if}}
{{/each}}

In this example all of the above reference the same prefix value even though they are located within different blocks. This behavior is new as of Handlebars 4, the release notes discuss the prior behavior as well as the migration plan.

Wilk answered 1/4, 2020 at 12:28 Comment(0)
C
1

Not an answer to the original question, but I faced a similar problem - I have a custom helper that was not rendering the variables inside of it.

My helper was similar to the handlebars if helper, so it should not have needed the ../path syntax. Turns out I was using a fat arrow function when registering the helper, and this is different inside a fat arrow function.

Changing this:

Handlebars.registerHelper('ifeq', (v1, v2, options) => {    

to:

Handlebars.registerHelper('ifeq', function (v1, v2, options)  {

fixed it for me

Commines answered 1/9, 2022 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.