hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Cleaner Erb Syntax #

by why in inspect

I know alot of you out there are using Erb syntax for your rthtml files in Rails. Erb is also used in Hobix and Soks. Well, did you know that you can mark Erb lines with an initial percent symbol, saving yourself the messiness of open and close brackets?

This:

 <% entries.each do |e| %>
   <div class="entryTitle"><%= e.title %></div>
   <div class="entryBody">
     <%= e.content.to_html %>
   </div>
 <% end %>

Can also be expressed as:

 % entries.each do |e|
   <div class="entryTitle"><%= e.title %></div>
   <div class="entryBody">
     <%= e.content.to_html %>
   </div>
 % end

I find it especially nice for loops, since I can easily browse the file for those marked lines. (More on Erb syntax in the docs.)

said on 09 Mar 2005 at 08:19

Fantastic, I didn’t know this. Very handy.

said on 09 Mar 2005 at 14:22

Can u also do

%= e.title

?

said on 09 Mar 2005 at 14:23

What do you do if you need a line with a percent? % puts '%', I guess…

said on 09 Mar 2005 at 15:26

undees: You can just do two percents.

said on 09 Mar 2005 at 15:27

It appears that rails does not have this enabled. Maybe somebody should submit a patch…

said on 13 Mar 2005 at 07:08

Yeah, this didn’t work in rails unfortunately

Comments are closed for this entry.