Last active
August 29, 2015 14:02
-
-
Save armandocanals/1e5955590d0947286f9a to your computer and use it in GitHub Desktop.
Micro-templates based off underscore templates
This file contains 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(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