Trim the fat with HAML
The announcement of Rails 1.2 wasn’t the only interesting post on weblog.rubyonrails.org, I also found out about HAML a templating system for Rails that cleans up your code and banishes those ugly ASP type tags.
If you look at the two partials of a form you’ll see that you not only save a few lines of code (and even more characters), but it’s much more legible and easier to maintain down the road.
<div class="block">
<h2>Details</h2>
<dl>
<dt class="required"><label for="country_continent_id">Continent:</label></dt>
<dd>
<%= f.collection_select(:continent_id, @continents, :id, :name) %>
</dd>
<dt class="required"><label for="country_name">Country:</label></dt>
<dd>
<%= f.text_field :name %>
</dd>
<dt class="required"><label for="country_content">Content:</label></dt>
<dd>
<%= f.text_area :content, :rows=>10 %>
</dd>
</dl>
</div>
pre-HAML _form.rhtml
.block
%h2 Details
%dl
%dt.required
%label{:for => "country_continent_id"} Continent:
%dd= f.collection_select(:continent_id, @continents, :id, :name)
%dt.required
%label{:for => "country_name"} Country:
%dd= f.text_field :name
%dt.required
%label{:for => "country_content"} Content:
%dd= f.text_area :content, :rows=>10
post-HAML _form.haml
In HAML divs are implicit so you can just type the class name (.block) and it will give you a div for free, tags start with ‘%’ (%p, %table, %h1) and are auto-closed.
HAML installs as a plugin and any .haml files will overide their .rhtml brothers. On the HAML site there’s a great tutorial and reference.
There’s already a TextMate bundle for HAML here.
This entry was written by
Alastair, posted on
January 22, 2007 at 9:02 am, filed under
Ruby on Rails. Bookmark the
permalink. Follow any comments here with the
RSS feed for this post.
or leave a trackback:
Trackback URL.
© Copyright 2006 - 2012 Alastair Dawson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
One Comment
hey there,
I absolutely LOVE the colour scheme you use for the syntax highlighting above.. Do you happen to have a Scintilla.NET XML file which holds those colours? Or do you have an easy accessible file for those colours. I’d love to use them during development!
Thanks!
Wes