Digging deeper into Rails programming styles
The last Sharing at Work post showed how to set up a Rails development environment using a Ubuntu Linux virtual machine. This week, I’m in the midst of developing my first ground-up public-facing web application using Ruby on Rails.
Learning a new way to dynamically generate HTML
My Rails development has already been aided in several places by kind folks in the #rubyonrails IRC channel on Freenode. One interesting thing they’ve directed me to is an alternate templating language called Haml. Rails uses something called “embedded Ruby” by default – .erb files that describe HTML output with some necessary Ruby interwoven to generate the dynamic parts of a page. haml does much the same but it takes a minimalist approach to syntax.
Nesting is handled by indentation (two spaces per level of nesting). Tags are not closed, the closer is simply inferred by the order in which they are used and the associated indentation.
To illustrate the goodness of haml I’ve borrowed some code snippets from a longer article on Hhaml at Rail Spikes.
Embedded Ruby example; 20 lines and 547 bytes:
This shows a stock offering div containing a name, a symbol, a the latest price and date of the latest trade, and some dynamically generated links to buy and sell the stock in question.
The same code using Haml; 13 lines and 365 bytes:
Click through to the original Sharing at Work post if these code snippets aren’t showing up on your screen.
What’s Haml doing for us?
You can see right away that the Haml code provides a lot less for the developer to read through and its appearance nicely dovetails with the CSS models we use to do page layout. To quote the Rail Spikes article linked above, “Hampton says he came up with Haml by starting with a fragment like this and seeing what he could take out without losing anything. I’ll follow his example.” I like it and I’m going to try to use this throughout my project. Maybe it will be useful to you too.
In the next post I’ll share what I’ve learned from Rails. Consumer web app development seems to be evolving faster than the SAP/Business Objects toolset I’ve been using at work.
Related articles by Zemanta
- Techmosis Rails (slideshare.net)
- Testing Rails Applications (timbauerblog.wordpress.com)
- JavaScript MVC (alistapart.com)
- SharePoint 2010 For Developers (slideshare.net)
![Reblog this post [with Zemanta]](http://img.zemanta.com/reblog_e.png?x-id=0b6ee93c-3d18-48ef-a804-1f9c5acd35a8)

