Posted By

emdin on 12/26/10


Tagged

template engine


Versions (?)

Simplest template engine


 / Published in: JavaScript
 

String template (String template, Object data)

Usage: template('Hello ${name}! (not ${name}?)', { name: 'Gandalf' })

Result: Hello Gandalf! (not Gandalf?)

  1. function template (template, data) {
  2.  
  3. var result = template;
  4.  
  5. for (var field in data) {
  6. var re = new RegExp( '\\$\\{' + field + '\\}', 'gi' );
  7. result = result.replace(re, data[field]);
  8. }
  9.  
  10. return result;
  11. }

Report this snippet  

You need to login to post a comment.