例如,以
objects作为持有财产
var an_array = [ {name: "My name"}, {name: "Another name"}];var source = ;var template = Handlebars.compile(source);var wrapper = {objects: an_array};console.log(template(wrapper));
并且您的模板可以如下使用此属性:
<ul> {{#each objects}} <li>{{name}}</li> {{/each}}</ul>
和演示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>
或者,您可以将数组直接传递给模板,并
each在上下文设置为
.(点)的情况下调用帮助程序
var template = Handlebars.compile(source);console.log(template(an_array));<ul> {{#each .}} <li>{{name}}</li> {{/each}}</ul>
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>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)