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.
-
-
class ActionView::Base
-
private
-
def convert_template_into_ruby_code
-
# dummy
-
end
-
-
def delegate_compile_with_preprocessing(handler, template)
-
-
if ::Erubis::Helpers::RailsHelper.preprocessing
-
preprocessor = ::Erubis::Helpers::RailsHelper::PreprocessingEruby.new(template)
-
template = self.instance_eval(preprocessor.src)
-
if ::Erubis::Helpers::RailsHelper.show_src
-
logger.debug "** Erubis: preprocessed==<<’END’\n#{template}END\n"
-
end
-
end
-
-
delegate_compile_without_preprocessing(handler, template)
-
end
-
-
alias_method_chain :delegate_compile, :preprocessing
-
end
-
-
require ‘erubis’
-
require ‘erubis/helpers/rails_helper’
-
-
module ActionView
-
module TemplateHandlers
-
class Erubis < TemplateHandler
-
-
def compile(template)
-
klass = ::Erubis::Helpers::RailsHelper.engine_class
-
properties = ::Erubis::Helpers::RailsHelper.init_properties
-
klass.new(template, properties).src
-
end
-
end
-
end
-
end
-
-
ActionView::Base.register_default_template_handler :erb, ActionView::TemplateHandlers::Erubis
-
ActionView::Base.register_template_handler :rhtml, ActionView::TemplateHandlers::Erubis
-
-
# settings
-
Erubis::Helpers::RailsHelper.engine_class = Erubis::FastEruby
-
Erubis::Helpers::RailsHelper.show_src = true
-
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.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.