Erubis

Posted by Nucc

I’ve been looking for memcache solutions, when I found merb. Merb is similar to Rails, it’s developed by Ezra Zygmuntowicz (author of Deploying Rails). Merb uses Erubis instead of ERB. Erubis is refinement of ERuby, three times faster than ERB, it has auto HTML escaping support, but the most important for me is the preprocessing ability.

I tried to install Erubis to Rails 2.0.2 for testing, but I had problems. I would like to share how to setup in Rails 2.0.2, because it’s not the same in 2.0.1 and 2.0.2

First, setup from gem

sudo gem install erubis --include-dependencies

We create /config/initializers/erubis.rb for loading on boot.

  1.  
  2. class ActionView::Base
  3.   private
  4.     def convert_template_into_ruby_code
  5.       # dummy
  6.     end
  7.  
  8.     def delegate_compile_with_preprocessing(handler, template)
  9.  
  10.       if ::Erubis::Helpers::RailsHelper.preprocessing
  11.         preprocessor = ::Erubis::Helpers::RailsHelper::PreprocessingEruby.new(template)
  12.         template = self.instance_eval(preprocessor.src)
  13.         if ::Erubis::Helpers::RailsHelper.show_src
  14.           logger.debug "** Erubis: preprocessed==<<’END’\n#{template}END\n"
  15.         end
  16.       end
  17.  
  18.       delegate_compile_without_preprocessing(handler, template)
  19.     end
  20.  
  21.     alias_method_chain :delegate_compile, :preprocessing
  22. end
  23.  
  24. require ‘erubis’
  25. require ‘erubis/helpers/rails_helper’
  26.  
  27. module ActionView
  28.   module TemplateHandlers
  29.     class Erubis < TemplateHandler
  30.  
  31.       def compile(template)
  32.         klass = ::Erubis::Helpers::RailsHelper.engine_class
  33.         properties = ::Erubis::Helpers::RailsHelper.init_properties
  34.         klass.new(template, properties).src
  35.       end
  36.     end
  37.   end
  38. end
  39.  
  40. ActionView::Base.register_default_template_handler :erb, ActionView::TemplateHandlers::Erubis
  41. ActionView::Base.register_template_handler :rhtml, ActionView::TemplateHandlers::Erubis
  42.  
  43. # settings
  44. Erubis::Helpers::RailsHelper.engine_class = Erubis::FastEruby
  45. Erubis::Helpers::RailsHelper.show_src = true
  46. Erubis::Helpers::RailsHelper.preprocessing = true

In settings, we have three options. I use FastEruby, and I turned on show_src to see precompiled html code in console. If you want to use preprocessing, need to set preprocessing true. Preprocessing is useful for loop, but the syntax changes, when you use it. Instead of <% %> you have to use [% %], and if you have variable, you have to use _?(’variable’). I tried to test in a view,

  1. <% 1.upto 1000 do %><%= link_to "My post", _?(’@post link’) %>

with ERB it’s generate 49-61 req/sec, erbius without preprocessing is 51-74 req/sec, and with preprocessor 90-120 req/sec. If I have more time, I try to make test cases, and publish them.

One comment: If you use html escaping function, change every <%= %> tags to <%== %> ;) I’ve need some time to realize my mistake.