Skip to content

Instantly share code, notes, and snippets.

@aziis98
Created December 18, 2016 20:12
Show Gist options
  • Save aziis98/d950ee1623f2a5f3a6b0b8a1908e5b42 to your computer and use it in GitHub Desktop.
Save aziis98/d950ee1623f2a5f3a6b0b8a1908e5b42 to your computer and use it in GitHub Desktop.
A very 14 line long template parser with handlebars like syntax. Requires jquery
function fromTemplate(templateId, data) {
data = data || {};
const templateCode = $('#' + templateId).html();
const compiledTemplate = templateCode
.replace(/\{\{(.+?)\}\}/g, function (match, code) {
return (function() {
return eval(code);
}.bind(data)());
});
return $.parseHTML(compiledTemplate.trim())[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment