Created
February 24, 2012 14:59
-
-
Save il-b/1901458 to your computer and use it in GitHub Desktop.
Include tag for underscore template
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
// 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