Reputation: 7900
I have this ItemView
in Marionette
:
App.View.MovieListItem = Marionette.ItemView.extend({
tagName: 'li',
className: 'movie',
model: App.Model.Movie,
id: function() {
return 'movie-'+this.model.get('imdb')
},
initialize: function () {
this.render();
},
template: _.template('<a href="javascript:;">'+
'<i class="fa fa-eye fa-3"></i>'+
'<span class="cover"></span>'+
'<strong><%= title %></strong>'+
'<small><%- year %></small>'+
'<small2>Cached</small2>'+
'</a>'),
});
And i want to know if it's possible to create the template dynamically?
Because some time i want(Base on method that check something) to remove the small2
tag.
Upvotes: 4
Views: 3346
Reputation: 5631
Marionette has a function called getTemplate
which can be used to return dynamic templates.
Example:
App.View.MovieListItem = Marionette.ItemView.extend({
tagName: 'li',
className: 'movie',
model: App.Model.Movie,
id: function() {
return 'movie-'+this.model.get('imdb')
},
initialize: function () {
this.render();
},
getTemplate: function(){
if(condition){
return _.template(/* HTML STRING */);
}else{
return _.template(/* ANOTHER HTML STRING */);
}
}
});
Upvotes: 11