Created
December 21, 2011 23:22
-
-
Save shinypb/1508178 to your computer and use it in GitHub Desktop.
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
| <!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