Skip to content

Instantly share code, notes, and snippets.

@armandocanals
Last active August 29, 2015 14:02
Show Gist options
  • Save armandocanals/1e5955590d0947286f9a to your computer and use it in GitHub Desktop.
Save armandocanals/1e5955590d0947286f9a to your computer and use it in GitHub Desktop.
Micro-templates based off underscore templates
template = function(text, data, translation) {
var matches = text.match(/\{\{([\s\S]+?)\}\}/g);
if (matches === null) { return text; }
if (translation !== undefined) {
for(key in translation) {
data[key] = translation[key];
}
}
for (var i = 0; i < matches.length; i++) {
var key = matches[i].match(/\{\{([\s\S]+?)\}\}/)[1].replace(/\s/g, ''),
val = (data[key]) ? data[key] : "";
text = text.replace(matches[i], val);
}
return text;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment