Skip to content

Instantly share code, notes, and snippets.

@jonbretman
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonbretman/8691892 to your computer and use it in GitHub Desktop.
Save jonbretman/8691892 to your computer and use it in GitHub Desktop.
Underscore templates with Mustache style tags and include functionality
var templates = {
register: function (key, template) {
this[key] = _.template(
'<% ' +
'var include=_.bind(function(k,d){print(this[k](d))},this);' +
'var includeEach=_.bind(function(k,d){_.each(d,function(v){print(this[k](v))},this)},this);' +
'var includeIf=_.bind(function(k,d){if(d)print(this[k](d))},this);' +
' %>' +
template,
null,
{variable: 'data'}
);
return this;
}
};
templates.register(
'person',
'<p><%= data.name %></p><% include("partials/snippet", data) %>'
);
templates.register(
'group',
'<p><%= data.name %></p><% include("person", data) %>'
);
templates.register(
'partials/snippet',
'<span>I am <%= data.snippet %></span>'
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment