Using Erubis in Rails

Posted by mig on May 07, 2008

Swapping out ERB for Erubis in a rails application is dead easy:

Install the gem:

  gem i erubis

Require it:

  # config/environment.rb
  require 'erubis/helpers/rails_helper'

Preprocessing is awesome:

  # config/environment.rb
  require 'erubis/helpers/rails_helper'
  Erubis::Helpers::RailsHelper.preprocessing = true

With that, you can use [%= %] inside your views instead of <%= %>. So doing this…

  [%= link_to "New Post", new_article_path %]

…will process the link_to when the template is loaded. However, when it comes time to render, what is being rendered is something more like this:

  <a href="/articles/new">New Post</a>

Which loads a lot faster than the ruby.

You can also preprocess enumerable statements, this is especially useful for something like this:

  [% Category.find(:all).each do |c| %]
    <li>[%= link_to c.name, category_path(c) %]</li>
  [% end %]

Very intense.

Trackbacks

Use this link to trackback from your own site.

Comments

Leave a response

Comments