inicio mail me! sindicaci;ón

Ruby Templating Engines - Do we really need them?

When I first learned about Ruby and Ruby on Rails, I quickly went through some sample code and looked at the syntax of the code. I must be honest and confess that the Ruby syntax was not what I was expecting, or used to. Coming from a strictly Java and PHP world, the lax syntax of Ruby was a big turn off to me. No curly braces? No semi-colons? What on earth is going on here?!?!

Well, I just needed to step out of my habit. I still haven't had much experience with Ruby, but I have come to accept the syntax and learn to work with it. I understand that Ruby allows you to use curley's and semi-colons, but when there is no strict structure enforced, you get different developers writing in different styles, and that get pretty ugly.

The next thing I looked at was how Rails handled its view in the Model-View-Controller paradigm. Being a front-end developer, the View is very important to me. When I first got involved with JSP's, I hated how developers would put scriptlets all over the html page and mix the logic in with the presentation. The same problems occured in PHP. Now here was a brand new framework on a relatively young language, and yet it seems like the same problems exist on Rails. Rails (and Ruby) use the same scriptlet syntax as asp, php, jsp, etc. To embed some logic or code into your html, you use < %= %> to print out a value and < % %> to process other Ruby code. To the defense of Rails, there isn't much "logic" within the html templates, but the scriptlet tags are still there and some reason it just bothers me.

Instead of complaining, I decided to see if there were any Ruby templating engines out there. I was glad to find quiet a few. Some good ones and some not so good. I found a nice listing of the more popular ones on this page:

http://www.rubygarden.org/ruby?HtmlTemplates

I haven't had the chance to delve into each one in detail, but from what I've read and the tutorials I've seen, my favorite ones are:

- Xtemplate http://xtemplate.sourceforge.net/index.html
- Amrita2 http://amrita2.rubyforge.org
- Iowa http://enigo.com/projects/iowa/index.html
- Kwartz http://www.kuwata-lab.com/kwartz/index.html

When I have a better understanding of each one, maybe I will create a comparison chart and discuss their advantages and disadvantages.

Lawrence Oluyede said,

April 29, 2005 @ 6:21 pm

I felt the same way, I come from Python and eRuby doesn't appeal to me much, but I can survive :)

I'm looking forward for the comparison!

Bye

Chris Edwards said,

May 4, 2005 @ 1:24 pm

You'll never be able to get away from presentation logic. This is different from application logic. With any templating system there will still be control logic and loops.

Ramin said,

May 4, 2005 @ 2:44 pm

Oh, I know you can never get away from presentation logic, but you can certainly seperate it from the presentation as much as possible. The way that Kwartz does this is by using the Pull, then Push method. It first pulls in your entire html template. This template will have unique ID's assigned to almost everything that needs to be dynamic. Kwartz then performs the logic necessary and then generates the output, which is a .rhtml file with html and ruby code mixed in. So the end result is still html code with ruby logic mixed in, but at least the presentation logic is kept in a seperate template, editable by hand, wysiwyg, whatever... w/o the ruby code mixed in.

I like Kwart's approach because the end result is embedded ruby code in html, which is the fastest result you can get, but it still manages to keep everything seperate. The only extra step required is to generate these rhtml files using the Kwarts generate scripts. This can be automated using some shell scripts or a build tool.

I'd like to also metion that Kwartz is able to generate not only ruby templates, but also jsp and php outputs from the same logic files! I will have more information on this once I do my full review.

RSS feed for comments on this post · TrackBack URI

Leave a Comment