Last active
December 14, 2015 01:29
-
-
Save ioncreature/5006783 to your computer and use it in GitHub Desktop.
Mustache grabbing example
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
/** | |
* @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; | |
}); | |
} | |
} | |
); |
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
<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