Created
November 24, 2021 06:05
-
-
Save sdon2/3dbb11c80a07e748c3e17eb5ca69adac to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| $.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