Created
December 18, 2016 20:12
-
-
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
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 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