Simple inline code in your rails templates

In: frontend

26 Oct 2009

when I write views and templates sometimes it occurs that I want to use some code like

<% if c = @customer[:image] %>; <img src='...' />; <% end %>

but obviously this code would look much more nice with short tags enabled, so I could use code like

% if c = @customer[:image] <img src='...' /> % end

but poorely, in a standard rails env this trim mode for ERb is not enabled by default.

For Rails 2.1.2, I've made it working by

# environment.rb: # ALLOW open ruby code lines inside templates ActionView::Base.erb_trim_mode = '-%'

At least for Rails 2.3.4 this does not works as a class is frozen error is raised. I'll post an update here, when I found out a simple solution to get this working in rails 2.3.x without the need to install the trim mode plugin.

Comment Form

You need to enable javascript in order to use Simple CAPTCHA.
Security Code: