Posted By

mattgay on 08/11/06


Tagged

javascript template


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

px


RND - simple template for innerHTML


 / Published in: JavaScript
 

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

  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  

You need to login to post a comment.