我有这样的骨干模型
var PeopleModel = Backbone.Model.extend({ defaults: { "people": [ { "username": "alan","firstName": "Alan","lastName": "Johnson","phone": "1111","email": "alan@test.com" },{ "username": "allison",firstName: "Allison","lastName": "House","phone": "2222","email": "allison@test.com" },{ "username": "ryan","firstName": "Ryan","lastName": "Carson","phone": "3333","email": "ryan@test.com" },{ "username": "ed","firstName": "Edward","lastName": "Feild","phone": "4444","email": "ed@test.com" },{ "username": "phil","firstName": "Philip","lastName": "Doom","phone": "5555","email": "phil@test.com" },{ "username": "gerald","firstName": "Gerald","lastName": "Butler","phone": "6666","email": "gerald@test.com" } ],"company": {"name": "Random Corp."},"country": "England" } });
以下是我的模板
<script id="people-template" type="text/x-handlebars-template"> {{#each people}} {{> person}} {{/each}} </script> <script id="person-partial" type="text/x-handlebars-template"> <div class="person"> <h2>{{fullName}} </h2> <div class="phone">{{phone}}</div> <div class="email"><a href="mailto:{{email}}">{{email}}</a></div> </div>
这是我如何实现部分使用handlebars.js.
我的问题
我们有类似的东西,我的意思是部分的下划线模板引擎?
2.如何在underscore.js模板引擎中实现部分
解决方法@H_301_18@
不,在Underscore的模板中没有本地的部分支持.但是,您可以将<%...%> ;;特别是你可以调用自己的函数,这样你可以添加一些部分的东西,而不会有困难.你可以有一个这样的模板:
<script id="people-template" type="text/x-handlebars-template">
<% _(people).each(function(person) { %>
<%= partial('person',person) %>
<% }) %>
</script>
window.partial = function(which,data) {
var tmpl = $('#' + which + '-partial').html();
return _.template(tmpl)(data);
};
演示:http://jsfiddle.net/ambiguous/HDuj5/9/
这不是像{{> …}}在Handlebars,但是Underscore的模板是一个非常薄的包装JavaScript本身,并限制了你的一些.您可以使用命名空间来避免将事情直接放在窗口中,也可以使用{variable: ...}
option to _.template
和包装器来设置您的标准助手.
<script id="people-template" type="text/x-handlebars-template"> <% _(people).each(function(person) { %> <%= partial('person',person) %> <% }) %> </script>
window.partial = function(which,data) { var tmpl = $('#' + which + '-partial').html(); return _.template(tmpl)(data); };
演示:http://jsfiddle.net/ambiguous/HDuj5/9/
这不是像{{> …}}在Handlebars,但是Underscore的模板是一个非常薄的包装JavaScript本身,并限制了你的一些.您可以使用命名空间来避免将事情直接放在窗口中,也可以使用{variable: ...}
option to _.template
和包装器来设置您的标准助手.