Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2010 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/748741 to your computer and use it in GitHub Desktop.
Save anonymous/748741 to your computer and use it in GitHub Desktop.
ultra-tiny javascript templating "system" (Mustache.js ultra-simplified)
var Templater = function(data, template) {
var output = "",
templatize = function(string, object) {
return string.replace(/\{\{([^\}]+)\}\}/g,
function(string, parens) { return object[parens]; })
};
this.render = function() {
for (var i = 0 ; i < template.length; i++) {
output += templatize(template[i], data) + "\n";
}
return output;
};
};
var exampleTemplate = [
"<html>",
" <title>{{title}}</title>",
" Hi {{name}}, it's {{day}}",
"</html>"
];
var exampleData = {title: "Some Web Page", name: "Giles", day: "Monday"};
new Templater(exampleData, exampleTemplate).render();
// result
// "<html>
// <title>Some Web Page</title>
// Hi Giles, it's Monday
// </html>
// "
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment