Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Marionette ItemView template dynamically

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.

like image 629
YosiFZ Avatar asked Dec 19 '22 14:12

YosiFZ


1 Answers

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 */);
        }
    }
});
like image 151
Cubed Eye Avatar answered Dec 24 '22 02:12

Cubed Eye