Skip to content

Instantly share code, notes, and snippets.

@ioncreature
Last active December 14, 2015 01:29
Show Gist options
  • Save ioncreature/5006783 to your computer and use it in GitHub Desktop.
Save ioncreature/5006783 to your computer and use it in GitHub Desktop.
Mustache grabbing example
/**
* @author Marenin Alexander
* February 2012
*/
define(
[
'components/mustache',
'text!./templates.htm'
],
function( mustache, templatesString ){
var templates = {};
if ( Object.keys(templates ).length === 0 )
grabTemplates( templatesString );
return {
/**
* @param {String} id - template identifier
* @param {Object?} data
* @returns String
*/
render: function( id, data ){
return mustache.render( templates[id], data, templates );
}
};
function grabTemplates( templatesString ){
var container = document.createElement( 'div' ),
scripts;
container.innerHTML = templatesString;
scripts = Array.prototype.slice.call( container.querySelectorAll('script[type="text/template"]'), 0 );
scripts.forEach( function( node ){
templates[node.id] = node.innerHTML;
});
}
}
);
<script id="ololo" type="text/template">
<div>ololo {{ pi }}</div>
</script>
<script id="trololo" type="text/template">
<div>troololo {{ pi }}</div>
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment