简体   繁体   English

每个循环访问 Handlebars.js 的 scope 之外的变量

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

I have a handlebars.js template, just like this:我有一个 handlebars.js 模板,就像这样:

{{externalValue}}

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

And this is the generated output:这是生成的 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.正如预期的那样,我可以访问myCollection的每个元素的idtitle字段来生成我的 select。 And outside the select, my externalValue variable is correctly printed ("myExternalValue").在 select 之外,我的externalValue变量被正确打印(“myExternalValue”)。

Unfortunately, in options' texts, externalValue value is never printed out.不幸的是,在选项的文本中, externalValue值永远不会被打印出来。

My question is: how can I access a variable outside the scope of the handlebars.js each from within the loop?我的问题是:如何从循环内访问每个 handlebars.js 的 scope 之外的变量?

I have a handlebars.js template, just like this:我有一个 handlebars.js 模板,就像这样:

{{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.正如预期的那样,我可以访问myCollection的每个元素的idtitle字段来生成我的选择。 And outside the select, my externalValue variable is correctly printed ("myExternalValue").在选择之外,我的externalValue变量被正确打印(“myExternalValue”)。

Unfortunately, in options' texts, externalValue value is never printed out.不幸的是,在选项的文本中, externalValue值永远不会打印出来。

My question is: how can I access a variable outside the scope of the handlebars.js each from within the loop?我的问题是:如何从循环中访问每个 handlebars.js 范围之外的变量?

I have a handlebars.js template, just like this:我有一个 handlebars.js 模板,就像这样:

{{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.正如预期的那样,我可以访问myCollection的每个元素的idtitle字段来生成我的选择。 And outside the select, my externalValue variable is correctly printed ("myExternalValue").在选择之外,我的externalValue变量被正确打印(“myExternalValue”)。

Unfortunately, in options' texts, externalValue value is never printed out.不幸的是,在选项的文本中, externalValue值永远不会打印出来。

My question is: how can I access a variable outside the scope of the handlebars.js each from within the loop?我的问题是:如何从循环中访问每个 handlebars.js 范围之外的变量?

I have a handlebars.js template, just like this:我有一个 handlebars.js 模板,就像这样:

{{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.正如预期的那样,我可以访问myCollection的每个元素的idtitle字段来生成我的选择。 And outside the select, my externalValue variable is correctly printed ("myExternalValue").在选择之外,我的externalValue变量被正确打印(“myExternalValue”)。

Unfortunately, in options' texts, externalValue value is never printed out.不幸的是,在选项的文本中, externalValue值永远不会打印出来。

My question is: how can I access a variable outside the scope of the handlebars.js each from within the loop?我的问题是:如何从循环中访问每个 handlebars.js 范围之外的变量?

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.我的助手类似于把手if助手,所以它不应该需要../path语法。 Turns out I was using a fat arrow function when registering the helper, and this is different inside a fat arrow function .原来我在注册助手时使用了粗箭头 functionthis粗箭头 function中是不同的。

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM