Vidar Hokstad V2.0

Home Blog

2008-04-12 11:19 UTC Mini reviews of 19 Ruby template engines

I've seen lots of template systems in Ruby, but haven't been really been happy with any of them. Over the last few months or so I've kept notes of the ones I looked at. This is the list I came up with, and some comments on each. But first a disclaimer:

I have NOT spent a lot of time looking at these - there are far too many for me to have time to go into a lot of detail for each of them. Some I know well, many I only know from a cursory review, so don't write an engine off just based on my comments. Also, I very much welcome corrections (or additions) in the comments

Wildcards

These don't fit in any of the other groups below.

  • Radius.
    Pro: Radius lets you define your own "tags" - the input is "almost XML" (it uses namespace prefixes, but relies on the prefix value, not the namespaceURI it's tied to - I don't know if that's intentional or lack of understanding of XML), which should let a lot of editors work well with it, and possibly (if you're careful) roundtrip it into web dev tools. It's also designed to be able to output any text, so it's not tied to HTML
    Con: It's not actually a templating language, but a tool for creating your own template systems (that certainly makes it more flexible, though).

  • HAML.
    Pro: Very compact syntax.
    Con: Whitespace matters. I ignore Python because of that (even though, as any Pythonista will tell you it's not nearly as bad as people say it is, which is probably true) - HAML would have to be a hell of a lot more compelling to overcome my innate dislike for that. It also just doesn't "feel right" to me. I'm a syntax snob, and not afraid to admit it: Either I like it or I don't. If I don't like the syntax I won't touch the language with a ten meter pole no matter what.

  • cs/Template.
    Pro: Fast (written in C). Uses paths into a tree of objects (like TAL/Amrita style engines). Not tied to HTML/XML. Almost safe to for external content (you'd need to disable "eval")
    Con: Not pure Ruby. Looks like you'd need to dip down into using the eval mechanism fairly frequently as control structures supported directly look fairly limited. Doesn't seem to allow user extension of the "macro"'s provided without dipping into the code, which would negate this point

eRuby style

Ruby code and text intervowen.

  • ERB
    Pro: Part of the Ruby standard library, and so it adds no external dependencies. Con: Templates are not much more than a string with Ruby interpolated. The Ruby is sprinkled in as "tags", but those tags are not valid XML tags, so editors that have strict XML modes will treat the tags as content (whether that's a good or a bad thing is purely subjective, I think - I don't like it). If you're using ERB, what's the benefit over just using standard Ruby interpolation syntax ( #{ ruby code here } )? Not much, as far as I can tell, other than some basic convenience functionality, and some very limited safety features.
  • eRuby
    Pro: Faster than ERB
    Con: External dependency. C extension
  • Erubis
    Pro: Faster than ERB / eRuby. Automatic escaping of variables possible
    Con: Same as for ERB
  • Galena
    Pro: Embed templates directly in your Ruby classes. If you for whatever reason don't want to split your views out from your code, Galena at least lets you format the text in a somewhat cleaner way than lots of variable interpolation.
    Con: Tight coupling of code and data. Even tighter than with "cleanly" written ERb if there is such a thing...
  • Tenjin
    Pro: Exists for multiple languages (Python, PHP, Perl, Javascript(!), Ruby, Java) with mostly the same syntax. Lots of features
    Con: Embedded code as the other ERb style languages, and this largely negates having it in multiple languages.

Liquid style

Text and "code" in a template language that is generally designed to be "safe" to varying degrees, up to and including allowing users to submit templated data without enabling execution of arbitrary code.

  • Liquid Markup.
    Pro: Aims to be safe, by preventing people editing the templates from running arbitrary code on your servers.
    Con: Another "language" to learn, though simple. Syntax for attributes etc. deviates from conventions shared by many other Ruby template languages.

  • Ruty
    Pro: Seems to be safe, by preventing arbitrary code or infinite loops. Very flexible set of filters. Template "inheritance". Mechanism to allow passing arbitrary "context" objects in without exposing unsafe methods to the temples.
    Con: Same as for Liquid

  • PageTemplate
    Pro: Seems to be safe. Looks like a sort of mix between a Liquid style template language and Textile style (apparently inspired by Perl's HTML:::Template, which I haven't used/looked at)
    Con: Not sure - personally the syntax didn't do it for me.

Amrita/TAL style

These look like pure XML (though many TAL-like template systems for Ruby and other languages violate XML expectations they are often close enough to be manipulated with a non-validating XML parser).

  • Amrita
    Pro: No code in the templates. Uses id's on the tags to tie the template to the model data. Can be "compiled" to Ruby like TAL
    Con: Uses REXML (slow) for XML parsing. Very basic functionality unless you provide Proc objects as part of the model data - like Radius it probably needs an extra layer to provide more advanced functionality for use in typical applications if you want to retain a clean model/view separation.
  • Amrita2
    Pro: "Compiles" to Ruby like PHPTal does rather than parsing templates each time. Much more flexible than the original Amrita. "Filters" can be applied to values to be output to handle rendering/formatting of values etc.
    Con: Complex (including supporting a really freaky syntax called AMXML as an alternative to the more TAL-like syntax). Seems to loose the relative safety of TAL-type templates by having several mechanisms for embedding Ruby. Too many ways of doing things.

  • Kwartz
    Pro: Exists in both a Ruby and PHP version using the same syntax for the templates, it seems. Appears to be very flexible. Should roundtrip easily to HTML editors. No logic at all in the templates Con: "Compiles" to ERb. No logic at all in the templates (yeah, I know, this can be a good thing too - depending on your use-case - the Kwartz approach is a bit extreme, though it seems). The logic/code uses a second template syntax instead of being just a file of valid code.

Markaby style

These tend to look like Ruby block notation, and make markup look purely like code.

  • Markaby.
    Pro: Markup as Ruby. Very simple and follows Ruby DSL conventions
    Con: Markup as Ruby... Inside a Markaby template "self" is the Markaby builder.
  • Maline.
    Pro: Simple to read; Follows fairly standard Ruby DSL conventions.
    Con: Does it actually offer anything "new" compared to Markaby and others that use almost exactly the same block syntax? I can't see any differentiating factors (I haven't looked very hard, though), but the author is clearly aware of Markaby (it's mentioned on the site), so I'm sure he has his reasons.
  • Builder::XmlMarkup
    Pro: Very small and simple. Comes bundled with Rails if that's what you're using.
    Con: Limited to XML generation (but that can of course also be a plus if it's just what you need)
    (Note: This originally said Builder was tied to Rails - corrected based on a comment below)

Wiki / Markdown / Textile style

These are "plaintext" or wiki style markup engines that aim to turn reasonably natural text into HTML or other output and may not have much in terms of variable expansion etc. as that's not their purpose. They will typically be unsuitable as a view templating system, but are useful as 'part of the arsenal' to handle markup of user generated content in a safe way.

  • RedCloth
    Pro: Textile for Ruby - "Plaintext like" / wiki type markup. Great for comments interfaces etc. to get mostly natural input without having to make people write HTML.
    Con: Not a full featured template language (which is ok - it's focus is on turning text into HTML). Syntax seems slightly out of tune with how I'd write plain text, and so it's still yet another markup to learn.
  • BlueCloth
    Pro: Markdown for Ruby - Platext / wiki type markup. Very similar to Textile / RedCloth in target, but to me Markdown syntax feels more natural for some types of text.
    Con: Not a full template language (which again isn't it's goal).

Seemingly dead projects (with or without code available)

In addition to the 19 above, I've come across a number of projects that appear to be dead, dormant or just haven't gotten properly started yet. I haven't looked more closely at them, but have included the links here. Descriptions are from their web-pages.


Comments

2008-04-12 13:21 UTC
Which templating engine would you recommend for s starter/beginner project with Rails?
2008-04-12 13:22 UTC
A big difference between ERB and "#{...}" interpolation is that with ERB you can install XSS Shield and enjoy a great deal of XSS-safety. You wouldn't have that with "#{...}" interpolation.
2008-04-12 14:02 UTC
Vidar Hokstad
Thomasz, Thanks for the comment - I personally really don't like the ERB style, but that at least gives one good reason.

Nick, I don't use Rails, so I don't know which of these fit well with Rails, though I know at least Liquid has a Rails plugin. If you're just starting out I'd probably suggest you stick with the "default" which is one of the ERb type engines, since that's what most examples etc. will be.

Personally I prefer engines that don't allow execution of arbitrary code. The Amrita/TAL style or Liquid style engines are probably my favorites at the moment - Ruty looks quite nice, but I haven't had time to go in depth.

2008-04-12 20:09 UTC
A quick correction -- while Builder::XmlMarkup is packaged with Rails, it's a stand-alone library: http://builder.rubyforge.org/

Other than that, thanks for the lineup! I've been meaning to check out some alternative template engines -- this makes that much easier.

2008-04-12 20:13 UTC
markus
Actually I think the whitespace issue with Python is in general exaggerated (and I am a ruby user).

Personally I believe no computer language _should_ indent with whitespace - it just feels so insignificant to bother about whitespace as a programmer. I also do not accept the argument of some python coders that this "will not matter in practise". I do not want to be forced by a parser to correctly indent. The parser is here to help humans, not the other way around.

I in fact DO intend my own code because I enjoy readability, but I would never accept that code fails to run because there is a wrong indent. This makes no sense to me.

However, with that being said, I think the whitespace critique of Python is really not that important after all. What is more important is how you think about problems, and when I enjoy working with objects, then I think that length(object) or __foobar__(self, argument1): simply feels extremely awkward. (By the way, even though I like ruby, I think the real way for OOP should be prototype based, not class based. Unfortunately I know of 0 prototype based languages with a good syntax. Python AND ruby have both got the syntax beauty part right, even though there are a few minor issues - take perl for a bad example of syntax "beauty".)

Anyway, I also write this because you wrote about HAML, and YAML actually also cares about whitespace stuff (which can be very annoying, I admit) - but I have totally fallen in YAML. It separates data from my ruby program. If i design a virtual army, I store the basic unit structure in yaml files. I love that approach.

Last but not least, i think the guy behind Kwart/rbtenjin is a very smart guy. I hope people push the tenjin stuff more, and i love that you can use or or anything else for that matter. :)

PS: With that being said, I think templates are in general rather ugly .....

2008-04-12 20:17 UTC
Jason Langenauer
I made the switch from ERb to HAML and I haven't looked back. I've never programmed in Python in my life, but I was able to get my head around the active whitespace of HAML after doing one view template (and thrashing my IDE into line to use spaces instead of tabs).

The HAML syntax is very compact, and I've found it's made my production and debugging of views much faster than with Erb. The only complaint I have is that the renderer's error messages can be sometimes cryptic, so you have sometimes try to fix things by trial and error.

Even so, I'd still highly recommend it.

2008-04-12 21:07 UTC
Vidar Hokstad
Markus/ Jason,

I actually agree with both of you that the Python whitespace isn't nearly as problematic as it's often presented. That said, it's problematic enough (in that enough people dislike it, whether it's just a minor nuisance or more) that it's a barrier to using Python for some people, me included.

I've never been able to get past it, personally. The entire approach just have given me enough distaste for Python that I don't want to bother.

If Python had been more compelling (if I for instance didn't have Ruby to play with instead), I might have been prepared to ignore it. As it is, I'm not, and I sort of feel the same way about Haml. If it'd given me more, maybe I'd overlook the whitespace "wart", but as it is, it doesn't look so amazingly fantastic that I'm willing to overlook something I consider that ugly. That isn't to say the terseness of it doesn't appeal on some levels - just not enough.

As for YAML, I hate YAML with a vengeance. I've talked to a lot of people that prefer YAML to XML for example because they think it's "more readable". To me YAML is less readable. With XML it's immediately obvious to me if the structure is right. With YAML an extra whitespace character here or there can totally mess things up. If people really need something simpler than well structured XML, then I'd even take .ini files over YAML any day, and certainly I could come up with a dozen different object serialization syntaxes I'd rather use than YAML (even JSON) for something "generic".

2008-04-12 21:08 UTC
Vidar Hokstad
Coda, thanks for the correction - I've updated the post.

2008-04-12 23:41 UTC
Alex
One more for the Wiki / Markdown / Textile category:

Texier (http://texier.rubyforge.org/), a Ruby implementation of Texy (http://texy.info/en/).

2008-04-13 02:47 UTC
There is also a Lilu rendering engine: http://gitorious.org/projects/lilu

(Some old article about it: http://www.infoq.com/news/2007/07/mockup-driven-dev-lilu)

2008-04-13 10:13 UTC
Trans
Just like to mention there is working rtals code and I will upload it soon, unfortunately:

1) It is slow

2) I don't have time to improve it

3) I really want this lib!

Oh, life's little contradictions ;) But if anyone would like to take it to the next level, I'd be more than happy to turn it over, help out when I can and even handle packaging and admin chores.

2008-04-13 19:23 UTC
Great write-up, Vidar - well done. And hey, didn't notice you have been blogging - we haven't really talked since the Edgeio days, hope you're doing well.
2008-04-13 20:39 UTC
David Mathers
There's also DRYML. It's currently integrated with the Hobo project, but they're saying it will be spun off soon into its own gem.

2008-04-13 20:46 UTC
> Which templating engine would you recommend for s starter/beginner project with Rails?

If you don't need to collaborate with Web designer, you'd better to use ERB which is the default template engine of Rails. It is the most trouble-less solution for Rails beginners.

If you need to collaborate with Web designer, I recommend you to use Kwartz, because Kwartz can generate ERB template from pure HTML file so it is easy to use in Rails, I think. http://www.kuwata-lab.com/kwartz/

2008-04-13 22:38 UTC
I used to feel pretty strongly about Python whitespace. HAML actually cured me of that. The big pluses I think haml gives you are

1) Compactness 2) Really nice looking output

Those are both big wins for me. Sure you can run your output through a post processor, but I get it for free with HAML.

Sure you have to deal with whitespace... but pretty much any editor (Textmate has a very useful HAML mode) should be able to deal with soft tabs at 2 spaces.

2008-04-14 04:29 UTC
You might also be interested in Ezamar, Ramaze's default templating engine. It's similar to the erb style engines, but lighter, with more of a direct interpolation feel.

Examples:

http://ramaze.net/getting-started#element.rb
http://ramaze.net/tutorials:todolist-9

Also, AFAIK erubis can be configured to use xml compliant tag markers.

2008-04-15 05:07 UTC
scoopr
And in the markaby category there is also remarkably. While no really apparent advantages, it also has a css builder in similiar style too.
2008-04-15 14:06 UTC
Moritz
I switched from ERB to HAML in my first days with Rails and I never looked back. Nice clean syntax and output. And since a while it has become quite fast. I dont see any reasons for not using HAML, except, as you mentionend, if you dont like the syntax and the intendation. At work i have to use Symfony which is normal HTML + a bit of PHP and i really really miss HAML here.
2008-04-16 14:50 UTC
For Markdown, I prefer Maruku ("a Markdown-superset interpreter") to BlueCloth: a lot (an order of magnitude or so, IIRC) faster, and seems less buggy (in BlueCloth, short words aren't transformed…).
2008-04-16 18:13 UTC
+1 HAML/SASS - Whitespace should matter ;)

I have also settled on Textile (RedCloth) as my current preferred markup.

2008-04-21 16:12 UTC
+1 for DRYML. Certainly people will argue about whether the approach is a good one. I find it helpful, especially in the beginning of a project (at least if you use it with the rest of Hobo) when you want to get things thrown in place quickly.

But it's not ready for production use unfortunately, which can be quite frustrating. Maybe by RailsConf it'll be more stable and reliable.

2008-04-28 14:10 UTC
I used to be with you on the whitespace thing, but after roughly 15 minutes of using HAML I've made a complete 180. Indention for blocks is so much clearer and less prone to confusion than {/} or begin/end.

This is one of those things where experience is the main factor. A lot of people are turned off by the idea just never try it, so never know what they actually think about it. It's kind of like people arguing against memory management. It seems absurd on the face of it, but once you've actually spent some time using it it's hard to imagine ever having to go back. You have no idea how much time you wasting hunting down closing tags (think moving code around from place to place) which ads exactly zero information to the code.

Which is to say, it's really worth keeping an open mind about indentation-as-scope.

2008-04-28 14:53 UTC
Vidar Hokstad
Oh, I've tried the whitespace thing, several times.

I really, really can't stand it. It looks ugly, and it feels wrong, and it just annoys the hell out of me.

It's a deal-breaker, and even more so because contrary to you I _like_ {/} and begin/end - they give me a clear visual cue, especially when combined with syntax highlighting, whereas indentation doesn't nearly as much (maybe influenced by working with lots of different languages with different indentation styles). An editor that gave stronger visual feedback on scope based on indentation might alleviate the latter, but it'd not be enough to convert me.

As for wasting time hunting down closing tags: I never do. Ever. A good editor handles that for you, just as most good editors will try to handle whitespace for you.

The problem with the latter is that a lot of people (me included) have very specific ideas about how things should be laid out, and end up battling with editors that tries to be too "smart" about it.

Post a Comment

Basic HTML allowed.

About me

E-mail: vidar@hokstad.com
Skype: vhokstad
View my LinkedIn profile

I was born April 21st, 1975, in Oslo, Norway. Since 2000 I've been living in London, UK. I'm married.

I'm working for Aardvark Media as Director of Technology. I'm also currently on the board of SpatialQ, a startup in the GIS space, and an advisor to Skoach, a startup doing a time management app for people with ADD.

Recent posts to my blog

Categories

StumbleUpon My link page

(Links I have stumbled and like)