[英]handlebars list all values into each
Using handlebars.js want to list all values from a key for each parent key, in my case tabs 使用handlebars.js想从每个父键的键列出所有的价值观,在我的情况标签
JSON: JSON:
{
"Default":{
"tabs": [{
"name":"About",
"tag":"[[ABOUT]]"
},{
"name":"Contact",
"tag":"[[CONTACT]]"
}]
}
}
TEMPLATE: 模板:
{{#tabs}}
<div class="box">
<input id="tab{{@index}}" type="radio" name="tabs" {{#if @first}}checked{{/if}} />
<div class="tabNames">
{{#each name}}
<label for="tab{{@index}}">{{@name}}<i></i></label>
{{/each}}
</div>
<section id="content{{@index}}">{{tag}}</section>
</div>
{{/tabs}}
I need this output: 我需要以下输出:
<div class="box">
<input id="tab1" type="radio" name="tabs" checked />
<div class="tabNames">
<label for="tab1">About</label>
<label for="tab2">Contact</label>
</div>
<section id="content1">[[ABOUT]]</section>
</div>
<div class="box">
<input id="tab2" type="radio" name="tabs" />
<div class="tabNames">
<label for="tab1">About</label>
<label for="tab2">Contact</label>
</div>
<section id="content2">[[CONTACT]]</section>
</div>
The problem is on #each name . 问题出在#each name上 。
Right now is returning only 1 x label and I need all of them. 现在只返回1 x标签,我需要所有这些。
Any help please? 有什么帮助吗?
You can't call #each
over a non-collection value like name (string). 您无法通过名称(字符串)之类的非集合值调用
#each
。 #each
is for iterating over an array or the properties of an object. #each
用于遍历数组或对象的属性。 You do not have a collection of names to iterate over. 您没有要迭代的名称集合。 You have an array of "tab" objects, each with a "name" property.
您有一个“选项卡”对象数组,每个对象都有一个“名称”属性。 Since you are already in the context of an element of the
tabs
array where you want to list all tab names, you will need to use a Handlebars path to get the tabs
array from the parent context: 由于您已经在
tabs
数组的元素中想要列出所有标签名称的上下文中,因此需要使用Handlebars路径从父上下文中获取tabs
数组:
{{#each ../tabs}}
<label for="tab{{@index}}">{{name}}<i></i></label>
{{/each}}
Note that you do not access a property like {{@name}}
, but, rather, like {{name}}
. 请注意,您不会访问
{{@name}}
类的属性,而是访问{{name}}
类的属性。
Also, in your example, all of your index start at 1 ( tab1
, tab2
). 同样,在您的示例中,所有索引均从1(
tab1
, tab2
)开始。 Arrays are 0-indexed, so you should expect your output to be 0-indexed as well: tab0
, tab1
, etc. 数组是0索引的,因此您应该期望输出也是0索引的:
tab0
, tab1
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.