Skip to content

Instantly share code, notes, and snippets.

@abriemme
Last active December 26, 2015 19:08
Show Gist options
  • Save abriemme/7198848 to your computer and use it in GitHub Desktop.
Save abriemme/7198848 to your computer and use it in GitHub Desktop.
Very very simple jQuery templating "engine"
function tmpl(template, data) {
return template.text().replace(/\{([\w\.]*)\}/g, function(str, key) {
var v = data[key];
return (typeof v !== "undefined" && v !== null) ? v : "";
})
}
/*
Usage:
html:
<script type="text/template" id="item-template">
<p>{firstName} {lastName}</p>
</script>
js:
$.ajax({
type: 'GET',
url: url,
success: function(response) {
$.each(response, function (index, item) {
$('#container').append(tmpl($('#item-template'), item))
})
},
error: function(e) {
console.error(e);
}
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment