Last active
June 10, 2018 16:12
-
-
Save ScalableJS/e683a9c8801be64b91e04417496c0a14 to your computer and use it in GitHub Desktop.
Nano Templater
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
var getTemplate = (function (html) { | |
var templates = {}; | |
//<([A-Z]\w+)[^>]*>([\s\S]*?)<\/([A-Z]\w+)[^>]*> | |
html.replace(/<template\s+data-template-name="(.+)"\s*>([\s\S]*?)<\/template>/g, function (str, name, value) { | |
templates[name] = value.replace(/([>{])\s+|\s+([<}])/g, '$1$2').replace(/\s+/g, ' '); | |
}); | |
return function (name, data) { | |
var template = templates[name]; | |
if (data) { | |
template = template.replace(/{{([^{}]+)}}/gm, function (match, p1) { | |
return data[p1]; | |
}); | |
} | |
return template; | |
} | |
}(html)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://jsfiddle.net/e6makt9p/16/