Last active
August 29, 2015 13:55
-
-
Save jonbretman/8691892 to your computer and use it in GitHub Desktop.
Underscore templates with Mustache style tags and include functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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