我有一个backbone.js/underscore.js 模板,我将其输入到主干视图中进行渲染。视图传递一个包含数组的模型posts
对象(我称之为post
在模板中)。
Problem:当我尝试循环遍历数组的所有元素时posts
,我收到一个错误Uncaught SyntaxError: Unexpected token )
并引用主干视图代码中的一行template: _.template( $('#tpl_SetView').html() )
.
我是否错误地执行了导致此错误的循环?
模板代码
<script type="text/template" id="tpl_SetView">
<div class="row_4">
<div class="photo_container">
<div class="set_cover">
<img src="/<%= posts[0].thumb_subpath %><%= posts[0].img_filename %>" width=240 />
</div>
<div class="set_thumbs">
<%= _.each(posts, function(post) { %>
<img src="<%= post.thumb_subpath %><%= posts.img_filename %>" width=55 />
<%= }); %>
</div>
</div>
</div>
</script>
要回显变量,请使用<%= %>
,但是要解析javaScript代码,只需使用<% %>
.
例如:
// In your Backbone View
var posts = {"posts": this.model.toJSON()};
var template = _.template($("#tpl_SetView").html(), posts);
// In your template
<div class="row_4">
<div class="photo_container">
<div class="set_cover">
<img src="/<%= _.escape(posts[0].thumb_subpath) %><%= _.escape(posts[0].img_filename) %>" width=240 />
</div>
<div class="set_thumbs">
<% _.each(posts, function(post){ %>
<img src="<%= _.escape(post.thumb_subpath) %><%= _.escape(posts.img_filename) %>" width=55 />
<% }); %>
</div>
</div>
</div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)