Posted By

kayue on 12/29/08


Tagged

template formate


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

ntulip
kayue


Templating/Formatting a String


 / Published in: JavaScript
 

URL: http://groups.google.com/group/jquery-ui/browse_thread/thread/45d0f5873dad0178/0f3c684499d89ff4?pli=1

"The {adjective} {color} {animal} jumps".template({adjective: 'quick',color: 'brown', animal: 'fox'}) ==> "The quick brown fox"

  1. String.prototype.template = function (o){
  2. return this.replace(/{([^{}]*)}/g,
  3. function (a, b) {
  4. var r = o[b];
  5. return typeof r === 'string' || typeof r === 'number' ? r : a;
  6. }
  7. );
  8. }

Report this snippet  

You need to login to post a comment.