Guest User

Untitled

a guest
Jul 22nd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. const self = {
  2.  
  3. /**
  4. * Finds and replaces stings formatted: <%KEY%> by a value with the same key
  5. */
  6.  
  7. engine: function (tpl, data) {
  8. // set the RegEx pattern
  9. var re = /<%([^%>]+)?%>/g, match;
  10. // Get the results of the expression
  11. while (match = re.exec(tpl)) {
  12. // Replace the string var with the designated value
  13. tpl = tpl.replace(match[0], data[match[1]])
  14. }
  15.  
  16. // Check if there are more variables to replace
  17. if (re.exec(tpl) !== null) {
  18. // if so, return an instance of itself.
  19. return self.engine(tpl, data);
  20. } else {
  21. // otherwise, return the string
  22. return tpl;
  23. }
  24. }
  25. }
  26.  
  27. module.exports = self;
Add Comment
Please, Sign In to add comment