Created
April 16, 2012 13:28
-
-
Save czottmann/2398816 to your computer and use it in GitHub Desktop.
Underscore.template: Super-simple/-stupid partials. It's quick and dirty yet it does what I want, so there.
This file contains hidden or 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
// Fetch the template HTML from the DOM, hand it over to `Underscore.template`, | |
// assign some variables, and set up a `partial` method. | |
var template = $("#tmpl-main-section").html(), | |
compiledTemplate = _.template( template, { | |
models: aCollectionOfThings, | |
// See template examples below on usage. It's all very complex. | |
partial: function( partialName, variables ) { | |
return _.template( | |
$( "#tmpl-" + partialName ).html(), | |
variables | |
); | |
} | |
}); | |
// Append the compiled template to the document body. | |
$("body").append(compiledTemplate); |
This file contains hidden or 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
<script type="text/html" id="tmpl-main-section"> | |
This is my main template. | |
<%= partial( "my-partial", { descriptor: "ossum partial" } ) %> | |
</script> | |
<script type="text/html" id="tmpl-my-partial"> | |
This is my <%= descriptor %>. | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment