Skip to content

Instantly share code, notes, and snippets.

@shinypb
Created December 21, 2011 23:22
Show Gist options
  • Select an option

  • Save shinypb/1508178 to your computer and use it in GitHub Desktop.

Select an option

Save shinypb/1508178 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<body>
<div id="templates" style="display:none;">
<div id="header" class="template"><!--
<i>{{awesomeGuy}} is awesome</i>
--></div>
<div id="body" class="template"><!--
<i>Hello, {{name}}!</i>
--></div>
<div id="footer" class="template"><!--
<i>{{neatGuy}} is neat is awesome</i>
--></div>
</div>
</body>
<script src="mustache.js"></script>
<script>
// Treats the contents of .template elements as templates
// Note: templates aren't necessarily valid HTML, so they need to be wrapped in <!-- comments -->
var partials = {};
Array.prototype.slice.call(document.getElementsByClassName("template")).forEach(function(elem) {
var name = elem.id, value = elem.innerHTML.replace('<!--', '').replace('-->', '');
partials[name] = value;
});
var data = {
awesomeGuy: 'Mark',
name: 'world',
neatGuy: 'Josh'
};
var html = Mustache(partials.main, data, partials);
document.write(html);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment