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

JavascriptTemplatesScopeEachhandlebars.js

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

Try

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

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

Solution 2 - Javascript

Or you can use absolute path like this:

<option value="{{id}}">{{title}} {{@root.user.path.to.externalValue}}</option>

Solution 3 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionlucke84View Question on Stackoverflow
Solution 1 - JavascriptspliterView Answer on Stackoverflow
Solution 2 - JavascriptkidkkrView Answer on Stackoverflow
Solution 3 - JavascripttitoihView Answer on Stackoverflow