json 对象的把手数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11582756/
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-09-03 18:27:58  来源:igfitidea点击:

handlebars array of json object

jsonhandlebars.js

提问by Stefano Maglione

i need to template with handlebars an array of json object:(by chrome console) [object,object,object,object] where every object is composed by this property:name,surname,ecc.

我需要用把手模板一个json对象数组:(通过chrome控制台)[对象,对象,对象,对象]其中每个对象都由这个属性组成:名称,姓氏,ecc。

I've understood that is impossible to put array of object in handlebars but we must create an unique object with all property of all object of array. Can anyone suggest me a function to create it

我知道不可能将对象数组放在车把中,但我们必须创建一个具有数组所有对象的所有属性的唯一对象。谁能建议我一个函数来创建它

回答by nikoshr

You could set your array as a property of a wrapper object when calling the template.

您可以在调用模板时将数组设置为包装器对象的属性。

For example, with objectsas the holding property

例如,objects作为持有财产

var an_array = [
    {name: "My name"},
    {name: "Another name"}
];

var source   = /* a template source*/;
var template = Handlebars.compile(source);
var wrapper  = {objects: an_array};

console.log(template(wrapper));

and your template can use this property as follows:

并且您的模板可以按如下方式使用此属性:

<ul>
    {{#each objects}}
        <li>{{name}}</li>
    {{/each}}
</ul>

And a demo http://jsfiddle.net/YuvNY/1/

还有一个演示http://jsfiddle.net/YuvNY/1/

var an_array=[
    {name:"My name"},
    {name:"Another name"},    
];

var source   = $("#src").html();
var template = Handlebars.compile(source);
$("body").append( template({objects:an_array}) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v2.0.0.js"></script>

<script type='text/template' id='src'>
<ul>
  {{#each objects}}
      <li>{{name}}</li>
  {{/each}}
</ul>    
</script>



Or you could pass directly the array to the template and call the eachhelper with the context set to .(a dot)

或者您可以直接将数组传递给模板,并each在上下文设置为.(一个点)的情况下调用助手

var template = Handlebars.compile(source);
console.log(template(an_array));
<ul>
    {{#each .}}
        <li>{{name}}</li>
    {{/each}}
</ul>

http://jsfiddle.net/nikoshr/YuvNY/32/

http://jsfiddle.net/nikoshr/YuvNY/32/

var an_array=[
    {name:"My name"},
    {name:"Another name"},    
];

var source   = $("#src").html();
var template = Handlebars.compile(source);
$("body").append( template(an_array) );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v2.0.0.js"></script>

<script type='text/template' id='src'>
<ul>
  {{#each .}}
      <li>{{name}}</li>
  {{/each}}
</ul>    
</script>