Templates in Prototype
March 17th, 2008As much as I love jQuery, prototype templates have made it into more than one of my projects because of their versatility and ease of use. They are a capable and amazing tool for formatting and displaying output on the client side and I thought I’d take a few minutes to dive into them.
So what are templates. In short they are a string with symbols embedded that are replaced at the time of evaluation to create a new string. For example:
var apple = { fruit: "apple", variety: "honeycrisp" };
var templ = new Template( "My favorite fruit are #{variety} #{apple}s." );
console.log( templ.evalaluate( apple ));
>>> My favorite fruit are honeycrisp apples.
This basic example is really all there is to templates. But, they can be incredibly useful for formatting and displaying JSON data. and repurposing things in universal ways across an app.


Previous Post
Next Post

1 Comment
March 20th, 2008 at 9:30 am
You might also want to check out Embedded JS (embeddedjs.com). It loads separate template files and provides rails-like helpers. Good stuff.
Leave a Reply