Skip to content

Instantly share code, notes, and snippets.

@ScalableJS
Last active June 10, 2018 16:12
Show Gist options
  • Save ScalableJS/e683a9c8801be64b91e04417496c0a14 to your computer and use it in GitHub Desktop.
Save ScalableJS/e683a9c8801be64b91e04417496c0a14 to your computer and use it in GitHub Desktop.
Nano Templater
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));
@ScalableJS
Copy link
Author

ScalableJS commented Sep 25, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment