Skip to content

Instantly share code, notes, and snippets.

@Fjan
Created November 20, 2010 00:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fjan/707461 to your computer and use it in GitHub Desktop.
Save Fjan/707461 to your computer and use it in GitHub Desktop.
Enable preprocessing in erubis with the <%=== %> directive
# Just drop this in the initializers directory of a rails 3 app and then use <%=== code %> in your templates
# and the code will be eval'ed at compile time, for example: <%=== link_to 'home', '/' %> actually puts
# <a href="/">home</a> in the compiled template avoiding the need to call link_to on every render
# This can provide a significant speedup of your rails views but beware that many rails helpers can produce
# different output depending on the context that is not available at compile time.
# For example, @form.text_field :name would produce different HTML if there was an error on the :name field.
# It doesn't work yet on asset tags like <%= javascript_include_tag :default %>
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment