Created
December 20, 2010 18:12
-
-
Save anonymous/748741 to your computer and use it in GitHub Desktop.
ultra-tiny javascript templating "system" (Mustache.js ultra-simplified)
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
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