Sun 29 Nov 2009
Haml in my Camel
Posted by nola under Ruby, Ruby On Rails
[2] Comments
Trying something new .. HAML... I saw it a couple years ago, I was like "ehh.. umm.. nah" ... But working on the DevChix site, it seems like it will be our choice instead of erb for templates so I thought I'd give it a try.
So here's some straight up html + erb
RUBY:
-
<h1>Listing users</h1>
-
-
<table>
-
<tr>
-
<th>Username</th>
-
<th>Email</th>
-
</tr>
-
-
<% @users.each do |user| %>
-
<tr>
-
<td><%=h user.username %></td>
-
<td><%=h user.email %></td>
-
<td><%= link_to 'Show', user %></td>
-
<td><%= link_to 'Edit', edit_user_path(user) %></td>
-
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
-
</tr>
-
<% end %>
-
</table>
-
-
<%= link_to 'New user', new_user_path %>
And same with haml:
RUBY:
-
%h1 Listing users
-
-
%table
-
%tr
-
%th Username
-
%th Email
-
-
- @users.each do |user|
-
%tr
-
%td
-
= user.username
-
%td
-
= user.email
-
%td
-
= link_to 'Show', user
-
%td
-
= link_to 'Edit', edit_user_path(user)
-
%td
-
= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete
-
-
= link_to 'New user', new_user_path
I am not totally sold, but.. it is an interesting kind of markup!
Let me know if you see a better way to write it or if you prefer another markup language!

November 30th, 2009 at 2:23 pm
I thought it looked bizarre and wrong for a while and then started liking it about a year ago. I’ve used it for a couple smallish projects and have been meaning to update my couple pre-existing Rails projects to use it. It’s just tidy and more like programming, though I admit it’s not a huge improvement.
December 5th, 2009 at 2:14 pm
Your Haml looks quite good. The only change I’d make is to move those
link_tos onto the same line as the%tds, like so:%td= user.username