Enabling preprocessing in Erubis makes views much faster

I wrote a 5 line hack that you can drop into your initializers directory to enable preprocessing in Erubis. This will run code in < %=== %> brackets at compile time making views considerably faster for rails helpers that only need to be evaluated once instead of on each render (the Erubis docs mention 20-40% speed increase but obviously it depends on the amount you precompile).

For example: <%=== password_field_tag :password %> will produce this *at compile time*: <input id="password" name="password" type="password" />

Of course, many helpers produce a different output depending on the context which is not available at compile time so for those you would need to stick with the usual <%= %>. For example link_to will work only if it does not need the action name to generate the route.

It doesn't work yet for "stylesheet_link_tag" and "javascript_include_tag" which is a shame because those would be excellent candidates to eval at compile time. Perhaps someone with a deeper knowledge of Rails can say what would be needed to make that work.

https://gist.github.com/707461

module ActionView   class Base     def get_binding;return binding;end   end

  class Template     module Handlers       class Erubis < ::Erubis::Eruby         def add_expr_debug(src, code)           src << "@output_buffer.safe_concat('" << escape_text(eval(code,ActionView::Base.new.get_binding).to_s) << "');"         end       end     end   end end