Jeremy Jeremy - 3 months ago 8
Javascript Question

Underscore/Backbone Templates .get

Is it possible to use

.get('property')
in an underscore/backbone template?

Example:

Passing data to the template:

var pageTemplate = _.template( PageTemplate, this);
$(this.el).html(pageTemplate);


The template it's self:

<div id="productContainer" style="left:<%= 1 + (450 - model.get('previewHeight'))/2 %>px;"></div>


Thanks.

Answer

Yes, it should be possible.

var my_json = {};
my_json.model = my_model;

var html_template = _.template($("#my_template").html());
$(this.el).html(html_template(my_json));

<div id="my_template" style="left:<%= 1 + (450 - model.get('previewHeight'))/2 %>px;"></div>