Skip to content

Instantly share code, notes, and snippets.

@sdon2
Created November 24, 2021 06:05
Show Gist options
  • Select an option

  • Save sdon2/3dbb11c80a07e748c3e17eb5ca69adac to your computer and use it in GitHub Desktop.

Select an option

Save sdon2/3dbb11c80a07e748c3e17eb5ca69adac to your computer and use it in GitHub Desktop.
$.template = function (templateName, data) {
var htmlSplits = $('script[type="text/html"][data-name="' + templateName + '"]').html().split(/\!\{[ ]*(.+?)[ ]*\}/g);
var render = function (props) {
return function (tok, i) { return (i % 2) ? props[tok] : tok; };
};
if (data) {
htmlSplits = htmlSplits.map(render(data));
}
return htmlSplits.join('');
};
/*
Usage:
<script type="text/html" data-name="hello-world">
<div class="sample" data-key="!{ key }">
Hello, World!!
</div>
</script>
<script>
var html = $.template("hello-world", { key: 1 });
console.log(html);
</script>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment