Last active
December 26, 2015 19:08
-
-
Save abriemme/7198848 to your computer and use it in GitHub Desktop.
Very very simple jQuery templating "engine"
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
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