mattmccray

 
« TaskTHIS is Back! | Home | Plausible Deniability… »

Liquid.js, A Non-Evaling Template Engine in JavaScript

» Published December 11, 2008 under Software

Of late, I’ve needed a robust JavaScript template engine that doesn’t use eval (and preferably not with). Also, I’ve always liked Liquid.

So, two plus two equals… JavaScript! Hah, you thought it was four, didn’t you? Well, that’s what you get for thinking.

Yeah, long story short, I ported Liquid to JavaScript. You can grab it from github (where else?) here: http://github.com/darthapo/liquid.js

I’m calling this version 0.1. It’s not battle tested yet, but I know it’ll work using Firefox 3+, Safari 3+, and Adobe Air 1.1.

So what are the differences between running the Ruby version and the JavaScript version? Well, as far as the templates themselves, nothing. It’s a full port, so all of Liquid’s default tags and filters are supported in Liquid.js. I even added a placeholder function that you can implement yourself (based on your own needs) to support the ‘include’ tag.

Liquid.readTemplateFile = function(path) {
  var elem = $(path);
  if(elem) {
    return elem.innerHTML;
  } else {
    return path +" can't be found."; 
    // Or throw and error, or whatever you want...
  }
}

var src = "{% include 'myOtherTemplate' with current_user %}";

var tmpl = Liquid.parse( src );

alert( tmpl.render({ current_user:'M@' }));

An easy way to include template in a page is to use script elements, like this:

<!-- Browsers ignore script blocks with an unrecognized type.  -->
<!-- Makes for pretty good inline template storage.  -->
<script type="text/liquid" id="myOtherTemplate">
  Hello, {{ current_user }}!
</script>

Currently, Liquid.js requires MooTools. Some of the things I’d like to polish up:

  • Remove MooTools requirement, run entirely independent of any other js library
  • Add Rhino for console-based testing
  • Test, test, test on Internet Exploder Explorer

2 comments

07.12.09 @ 19:05 Joel Nation said...
This saved me a lot of problems with our Air based app. This is the only non-eval templating engine I could find.

Anyway, this doesn’t fully work in IE (IE6 anyway, didn’t try the others). The data is added, but none of the commands are recognised (eg: if, for etc).

I got it to work after using this script http://blog.stevenlevithan.com/archives/.. to fix the problems in IE’s split function. Now it works great in IE!
03.05.10 @ 09:53 CarolynYates25 said...
Some time before, I needed to buy a good car for my corporation but I did not have enough money and couldn’t buy something. Thank God my comrade suggested to get the business loans from creditors. Therefore, I acted so and was satisfied with my student loan.

No trackbacks

Trackback link:

Please enable javascript to generate a trackback url


You may use Textile, or simple html tags (B,I). Feel free to use Emoticons too. Oh, and please limit yourself to only five links per comment. Anything more and you'll probably get detained by the spam police.