RND - simple template for innerHTML


/ Published in: JavaScript
Save to your folder(s)

Simple templating for innerHTML functions. Parses an input string (tmpl) for %(NAME) tokens, accepts an object (ns) as data and returns an output string.


Copy this code and paste it in your HTML
  1. function RND(tmpl, ns) {
  2. var fn = function(w, g) {
  3. g = g.split("|");
  4. var cnt = ns[g[0]];
  5. for(var i=1; i < g.length; i++)
  6. cnt = eval(g[i])(cnt);
  7. return cnt || w;
  8. };
  9. return tmpl.replace(/%(([A-Za-z0-9_|.]*))/g, fn);
  10. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.