Javascript 在dust js中为数组使用动态变量名
我的模型看起来像:Javascript 在dust js中为数组使用动态变量名,javascript,dust.js,Javascript,Dust.js,我的模型看起来像: var model = { tagTypes : ["product", "location"], cards : [{ title : "Card 1", product : ["prod1"], location : ["US", "UK"] }, { title : "Card 2", product : ["prod2"], locatio
var model = {
tagTypes : ["product", "location"],
cards : [{
title : "Card 1",
product : ["prod1"],
location : ["US", "UK"]
},
{
title : "Card 2",
product : ["prod2"],
location : ["UK"]
}]
};
我正在将我的“卡”(即本例中的产品和位置)的字段(这是一个动态列表,可能会根据响应而变化)作为数组(标记类型)
我想逐一遍历这些字段的元素。我可以使用当前的dust模板以序列化的形式获取这些列表。但我想实际循环遍历每个单独的元素,以便在它们之间添加一些额外的html
当前灰尘模板:
{#cards}
{title} |
{#tagTypes card=.}
{card[.]}
{/tagTypes}
<br/>
{/cards}
{#cards}
{title} |
{#tagTypes card=.}
{.}:
{#garbage tagType=.}
{#card[tagType]}
<span> {.} </span>
{/card[tagType]}
{/garbage}
{/tagTypes}
<br/>
{/cards}
所需产量(天):
卡1 |产品:产品1位置:美国-英国
卡2 |产品:prod2位置:英国
请参阅:以了解对上述代码的试验 问题是由于在单个灰尘标签中同时使用散列和点(灰尘不支持) 为了解决这个问题,我在模型中使用了一个可用的标量&创建了一个带有参数的部分,该参数引用了对象所需的键。这消除了点运算符的使用,它允许我安全地使用散列 模型
var model = {
tagTypes : ["product", "location"],
garbage : "foo",
cards : [{
title : "Card 1",
product : ["prod1"],
location : ["US", "UK"],
},
{
title : "Card 2",
product : ["prod2"],
location : ["UK"]
}]
};
工作模板:
{#cards}
{title} |
{#tagTypes card=.}
{card[.]}
{/tagTypes}
<br/>
{/cards}
{#cards}
{title} |
{#tagTypes card=.}
{.}:
{#garbage tagType=.}
{#card[tagType]}
<span> {.} </span>
{/card[tagType]}
{/garbage}
{/tagTypes}
<br/>
{/cards}
{#cards}
{title}|
{#标记类型卡=。}
{.}:
{#垃圾标记类型=。}
{#卡片[tagType]}
{.}
{/card[tagType]}
{/垃圾}
{/tagTypes}
{/cards}
工作区: