Handlebars使用动态索引访问数组 [英] Handlebars array access with dynamic index

查看:238
本文介绍了Handlebars使用动态索引访问数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用变量而不是硬编码值访问句柄模板中的数组元素?
我需要做以下事情:

How can I access to an array element inside handlebars template using a variable instead of an hardcoded value? I need to do something like:

{{#each condition in conditions}}
    {{App.ops.[condition.op].name}}
{{/each}}



<目前并没有给我一个解析错误,但在运行时不会返回任何东西。
如果我这样做:

at the moment doesn't give me a parse error but on runtime doesn't return me nothing. If i do something like this:

{{App.ops.[1].name}}

它可以工作,但它不是我要找的东西

it works but it's not what i'm looking for

推荐答案

有关我对另一个问题的回答

您可以使用内置查找助手
$ b

You can use the built-in lookup helper:


lookup helper允许使用Handlebars变量进行动态参数解析。这对于解析数组索引的值很有用。

The lookup helper allows for dynamic parameter resolution using Handlebars variables. This is useful for resolving values for array indexes.

使用 lookup 例子可以写成:

Using lookup, your example could be written as

{{#each condition in conditions}}
    {{#with (lookup ../App.ops condition.op)}}
        {{name}}
    {{/with}}
{{/each}}

(请注意,如果不知道数据结构,我将假定 App.ops 。)

(Note that without knowledge of the structure of your data, I'm making an assumption about the location of App.ops.)

这篇关于Handlebars使用动态索引访问数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆