【发布时间】:2018-07-07 17:23:29
【问题描述】:
我是主干 js 的新手...我有一个应用程序,我需要在主干视图中的两个不同 html 模板之间切换。我应该如何在单个视图中定义这些模板并根据我的条件在该模板上渲染函数。
【问题讨论】:
标签: html templates laravel-5 backbone.js view
我是主干 js 的新手...我有一个应用程序,我需要在主干视图中的两个不同 html 模板之间切换。我应该如何在单个视图中定义这些模板并根据我的条件在该模板上渲染函数。
【问题讨论】:
标签: html templates laravel-5 backbone.js view
您可以通过以下方式渲染两个不同的模板
<div>
<%if(condition){%>>
<div>Template 1</div>
<%}else{%>
Template 2
<%}%>
</div>
【讨论】:
var PersonView = Backbone.View.extend({
tagName: 'li',
template_1: _.template("<strong><%= name_1 %></strong> (<%= age_1 %>) - <%= occupation_1 %>"),
template_2: _.template("<strong><%= name_2 %></strong> (<%= age_2 %>) - <%= occupation_2 %>"),
initialize: function(){
this.render();
},
render: function(){
if (condition) {
this.$el.html( this.template_1(this.model.toJSON()));
} else {
this.$el.html( this.template_2(this.model.toJSON()));
}
}
});
【讨论】: