Simplest template engine


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

*String template (String template, Object data)*

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

Result: Hello <b>Gandalf!</b> (not Gandalf?)


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.