Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
view helpers for underscore
var data = {
type: "something",
name: "whatever",
addresses: [
{
type: "read_address",
address: "1/2/3",
value: "10"
},
{
type: "write_address",
address: "1/2/4"
}
]
}
<script id="my-template" type="text/template">
<p>type: <%= type %></p>
<p>name: <%= name %></p>
<p>read address: <%= _.find(this.addresses, function(addr){ return addr.type == "read_address" }).address %> </p>
<p>read address value: <%= _.find(this.addresses, function(addr){ return addr.type == "read_address" }).value %> </p>
<p>write address: <%= _.find(this.addresses, function(addr){ return addr.type == "write_address" }).address %> </p>
</script>
var viewHelpers = {
getAddress: function(type){
var address = _.find(this.addresses, function(addr) {
return addr.type == type
});
return address;
}
}
_.extend(data, viewHelpers);
_.template(myTemplate, data);
<script id="my-template" type="text/template">
<p>type: <%= type %></p>
<p>name: <%= name %></p>
<p>read address: <%= getAddress("read_address").address %> </p>
<p>read address value: <%= getAddress("read_address").value %> </p>
<p>write address: <%= getAddress("write_address").address %> </p>
</script>
Backbone.View.extend({
template: "#my-template",
render: function(){
var data = this.model.toJSON();
_.extend(data, viewHelpers);
var html = _.template($(this.template), data);
this.$el.html(html);
}
});
Backbone.Marionette.ItemView.extend({
template: "#my-template",
templateHelpers: viewHelpers
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.