Skip to content

Instantly share code, notes, and snippets.

@czottmann
Created April 16, 2012 13:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save czottmann/2398816 to your computer and use it in GitHub Desktop.
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.
// 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);
<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