Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const self = {
- /**
- * Finds and replaces stings formatted: <%KEY%> by a value with the same key
- */
- engine: function (tpl, data) {
- // set the RegEx pattern
- var re = /<%([^%>]+)?%>/g, match;
- // Get the results of the expression
- while (match = re.exec(tpl)) {
- // Replace the string var with the designated value
- tpl = tpl.replace(match[0], data[match[1]])
- }
- // Check if there are more variables to replace
- if (re.exec(tpl) !== null) {
- // if so, return an instance of itself.
- return self.engine(tpl, data);
- } else {
- // otherwise, return the string
- return tpl;
- }
- }
- }
- module.exports = self;
Add Comment
Please, Sign In to add comment