Skip to content

Instantly share code, notes, and snippets.

@unicodefreak
Created February 24, 2012 14:59
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unicodefreak/1901458 to your computer and use it in GitHub Desktop.
Save unicodefreak/1901458 to your computer and use it in GitHub Desktop.
Include tag for underscore template
// based on http://emptysquare.net/blog/adding-an-include-tag-to-underscore-js-templates/
// include tag for underscore templates
// <% include template-id %>
var _underscore_template = _.template;
_.template = function(str, data) {
// match "<% include template-id %>"
return _underscore_template(
str.replace(
/<%\s*include\s*(.*?)\s*%>/g,
function(match, templateId) {
var el = $('#' + templateId);
return el ? el.html() : '';
}
),
data
);
};
/* templates
<script type="text/template" id="foo">
foo
</script>
<script type="text/template" id="bar">
<% include foo %>
bar
</script>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment