Patroon - a Javascript Template Engine

Patroon is a template engine written in Javascript in about 130 lines of code. It takes existing DOM nodes annotated with class names and expand a data object according to simple rules. Additionally you may use traditional string interpolation inside attribute values and text nodes.

The Data

Comments in this blog are stored as a list of JSON objects, I wrote about it here. So think about a data object like this:

  1. var data = {
  2. comment: [{
  3. time: "2008-09-07 12:28:33",
  4. name: "David Beckham",
  5. website: "beckham.com",
  6. text: "I watched the euro finals on tv..."
  7. }, {
  8. time: "2008-09-07 14:28:33",
  9. name: "Tuncay",
  10. website: "",
  11. text: "Me too"
  12. }]
  13. };

The Template

This data will be expanded with help of following template:

  1. <div class="comments">
  2. <div id="comments-template">
  3. <div class="comment">
  4. <div class="top">
  5. {website.length > 0 ? linkTo(name, website) : name} said
  6. <a title="{time}"></a>:
  7. </div>
  8. <div class="text">
  9. {text}
  10. </div>
  11. </div>
  12. </div>
  13. </div>

Usage

The javascript to actually execute this template looks like this:

  1. // The comments template will be removed from the DOM!
  2. var template = new Template('comments-template');
  3. // Expand the template into the comments section
  4. $('.comments').expand(template, data);

If you don’t want to use jQuery, please look at the end of this article.

Output

The given example renders following output:

  1. <div class="comments">
  2. <div id="comments-template">
  3. <div class="comment">
  4. <div class="top">
  5. <a href="http://backham.com">David Beckham</a> said
  6. <a title="2008-09-07 12:28:33">2 hours ago</a>
  7. </div>
  8. <div class="text">
  9. I watched the euro finals on tv...
  10. </div>
  11. </div>
  12. <div class="comment">
  13. <div class="top">
  14. Tuncay said
  15. <a title="2008-09-07 14:28:33">1 minute ago</a>
  16. </div>
  17. <div class="text">
  18. Me too
  19. </div>
  20. </div>
  21. </div>
  22. </div>

Basic Rules

There are 3 basic rules regarding the evaluation:

Helper

Code snippets inside the template will be executed within the scope of a Helper object. If you want to extend it, just add your functions to Template.Helper. At the moment it defines only one function:

  1. Template.Helper = {
  2. linkTo: function(text, url) {
  3. if (url.indexOf('http://') == -1 && url[0] != '/' && url[0] != '#') {
  4. url = 'http://' + url;
  5. }
  6. return '<a href="' + url +'">' + text + '</a>';
  7. }
  8. };

Examples

Download

Download the script at my github repository.

Related Work

There are some other libraries for javascript templating, which are related to Patroon:

Patroon is probably the smallest templating solution around and consists only of 130 lines of code.

Appendix

Without jQuery template expansion is a bit verbose:

  1. // The comments template will be removed from the DOM!
  2. var template = new Template('comments-template');
  3. // template will result in a new DOM node
  4. var result = template.expand(data);
  5. // insert the resulting node into the comments container
  6. var container = document.getElementsByClassName('comments')[0];
  7. container.appendChild(result);