Skip to content

Instantly share code, notes, and snippets.

@Andrew8xx8
Created May 14, 2012 15:34
Show Gist options
  • Save Andrew8xx8/2694600 to your computer and use it in GitHub Desktop.
Save Andrew8xx8/2694600 to your computer and use it in GitHub Desktop.
Basic JS templater
/*
Usage:
fetchTemplate('#hello_world', {
h: 'Hello',
w: 'world',
})
<!DOCTYPE HTML>
<html>
<body>
<div id="hello_world">
{h} <strong>{w}</strong>!
</div>
</body>
</html>
*/
var fetchTemplate = function(template_name, data){
var result = $(template_name).html();
for(var i in data) {
var name = new RegExp('{' + i + '}','g');
result = result.replace(name, data[i]);
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment