【发布时间】:2015-10-03 11:32:39
【问题描述】:
为了使用主干/下划线模板,我有一个非常简单的代码。
HTML:
<script type="text/template" id="search_template">
<div>Name comes here: <h4><%=name%></h4></div>
<input type="text" id="search_input" />
<input type="button" id="search_button" value="Search" />
</script>
<div id="search_container"></div>
JS:
$(function(){
var SearchView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
var tmpl = $('#search_template').html(),
compiled = _.template(tmpl, { name:'hello' });
$(this.el).html(compiled);
}
});
var search_view = new SearchView({ el: "#search_container" });
});
问题是它看不到应该传递给模板的键“名称”。我还是不明白为什么。
整个代码示例位于此处:http://plnkr.co/edit/7fV2azTh6cpjmUxIBHvJ?p=preview
【问题讨论】:
标签: javascript backbone.js underscore.js templating