The way to the ideal template engine for rails
This post is still under development! Come back for updates!
Motivation
For our new Project at Datanion we need a template engine for Rails that hits the following requirements:
- Up to date technology, this includes Rails 2.0 of course. A plus is active development of more than one guy
- Easy to use for both, the developer ( me
) and the designers - Scalability
- The option to disable e-ruby parsing or at least allow only a very downstripped version of it
- The option to use different themes, each instance of the application will have its own
layout.htmland differentviews. Perhaps, but not limited, with the use of the theme_support or the MultiSite plugin
The candidates
There are different template engines out there. These are the candidates I tested in the order I checked them:
- Liquid – Liquid is an extraction from the e-commerce system Shopify. Shopify powers many thousands of e-commerce stores which all call for unique designs. For this we developed Liquid which allows our customers complete design freedom while maintaining the integrity of our servers.
- Masterview – MasterView is a ruby/rails optimized HTML/XHTML friendly template engine. It is designed to use the full power and productivity of rails including layouts, partials, and rails html helpers while still being editable/ styleable in a WYSIWYG HTML editor.
- Amrita2 – Amrita2 is a a xml/xhtml template library for Ruby. It makes html documents from a template and a model data.
- Kwartz – Kwartz is a template system which realized the concept of ‘Independence of Presentation Logic’ (IoPL).
- PageTemplate – PageTemplate is a Ruby package which allows you to utilize text templates for your Web projects.
The pros and cons of each of them
Liquid
I think Liquid is the most popular template engine. Developed for shopify it is in production use since June 2006 and is now used by many other hosted web applications. One could imagine that this is a pro for this engine, but my personal experiences are slightly different! The documentation is really lacking, sometimes outdated! The wiki contains code examples that will not run the way you expect:
Hello tobi has {{ 'tobi' | length }} letters! # --> Hello tobi has tobi letters
Hello {{ '*tobi*' | textilize | upcase }} # --> Hello *TOBI*
The length bug is known, but it has not been fixed till now. At least the tiny “using liquid templates” wiki entry should be bug free.
Masterview
Well, not much to say… Masterview isn’t Rails 2.0 ready:
If you would send us any Rails 2.0 changes you find, then we'd be glad to get them in there.
Even better, if you want to make the changes to the code and send us diff's along with a short explanation of each change, we can apply things even quicker. Ideally with test updates too, but we'll take whatever we can get
That is really pity because this engine has a nice feature: Designers can use their favourite WYSIWYG-editor to edit the templates, and test them locally without any aditional software installed! It would even be possible to use Masterview in combination with Liquid. The developer made a really nice presentation (MasterView: Evolution of a WYSIWYG Template Engine) and a sreencast. I am discussing the ability of Masterview to play together with the theme_support plugin with the author here.
Amrita2
This engine is not Rails specific so it runs natively in every Ruby application, but has a ‘RailsBridge’ to communicate with your apps easily. Amrita2 just switched to Rails 2.0, so the Rails specific documentation is still buggy and the samples are not in Rails 2.0 style (e.g. list.a2html instead of list.html.a2). The idea behind Amrita2 is really similar to the one of Masterview: As the author writes, the template can be written by designers using almost any xhtml/xml editor and the output is controlled by data not by logic. So it’s easy to write, test and debug the code.
### controllers/application.rb
class ApplicationController < ActionController::Base
include Amrita2
end
### controllers/foos_controller.rb
class FoosController < ApplicationController
def index
@title = 'Hello ROR with Amrita2'
@body = 'Using Amrita2 with ROR is easy and fun!'
end
end
### views/foos/index.html.a2
< h1 am:src='title'>hello world< /h1>
< p am:src='body'>Amrita2 is a html template library for Ruby< /p>
Kwartz
First of all, I think Kwartz is fun to use. The documentation is really good and it would be easy to use for the developer and also the designers. But there is a deficit making it useless for me: Kwartz is not Rails 2.0 ready. I emailed the author of Kwartz and asked about Rails 2.0 support. I’ll keep you updated!
PageTemplate
PageTemplate doesn’t have a ‘rails bridge’ so you have to do this on your own:
### controllers/application.rb
class ApplicationController < ActionController::Base
require 'PageTemplate'
end
### controllers/foos_controller.rb
class FoosController < ApplicationController
def index
template = PageTemplate.new()
template.load("#{RAILS_ROOT}/app/views/foos/index.html.erb")
template['title'] = 'My PageTemplate Script'
render :text => template.output, :layout => true
end
end
### views/foos/index.html.erb
[%var title%]
I would consider this as a core-template engine, without any knickknack. If you like to write your own Rails template engine take PageTemplate as lesson
Conclusion
comming soon...
February 20th, 2008 at 18:19
Dear Johannes Leers,
Thank you for linking to AdaRuby
April 9th, 2008 at 21:19
We like your blog!…
[...]Mabuhay, my colleagues and I heard of your blog over at McBrides, so we thought we would take a look. We’ve read several of your posts and we all agree that you have a fine writing style[...]
…
April 23rd, 2008 at 03:09
I found your site on faves.com bookmarking site.. I like it ..gave it a fave for you..ill be checking back later
March 6th, 2009 at 03:55
Johannes, I’m using Kwartz in production with Rails 1.2.3. However, we’re to a point where we have to upgrade to the latest Rails and I’m in the process of refactoring Kwartz to get it to work. Email me if you’re interested and I’ll send you what I have when I’m done.