Javascript 每个循环访问 Handlebars.js 范围之外的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13645084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 12:34:22  来源:igfitidea点击:

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

javascripttemplatesscopeeachhandlebars.js

提问by lucke84

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 idand titlefields of every element of myCollectionto generate my select. And outside the select, my externalValuevariable is correctly printed ("myExternalValue").

正如预期的那样,我可以访问每个元素的idtitle字段myCollection来生成我的选择。在选择之外,我的externalValue变量被正确打印(“myExternalValue”)。

Unfortunately, in options' texts, externalValuevalue 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 范围之外的变量?

回答by spliter

Try

尝试

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

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

../路径段引用父模板范围,应该是你想要的。

回答by kidkkr

Or you can use absolute path like this:

或者你可以像这样使用绝对路径:

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

回答by titoih

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

我看到许多与 404 相关的链接,以获取有关此主题的文档。

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

我用这个更新了它,它在2020 年 4 月 1 日工作:

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

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.

一些像 #with 和 #each 这样的助手允许你深入到嵌套对象中。当您将 ../ 段包含到您的路径中时,Handlebars 将变回父上下文。

    {{#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.

../ 将解析为的确切值因调用块的助手而异。仅当上下文发生变化时才需要使用 ../。诸如 {{#each}} 之类的助手的子代需要使用 ../ 而诸如 {{#if}} 之类的助手的子代则不需要。

{{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 notesdiscuss the prior behavior as well as the migration plan.

在此示例中,即使它们位于不同的块中,上述所有内容也引用相同的前缀值。此行为是 Handlebars 4 的新行为,发行说明讨论了先前的行为以及迁移计划。