Skip to content

Instantly share code, notes, and snippets.

@greendog
Created January 23, 2013 12:54
Show Gist options
  • Save greendog/4605222 to your computer and use it in GitHub Desktop.
Save greendog/4605222 to your computer and use it in GitHub Desktop.
Create a Liquid Handler for Rails 3.1 Use: require 'action_view/template/handlers/liquid' ActionView::Template.register_template_handler :liquid, ActionView::Template::Handlers::Liquid
class ActionView::Template::Handlers::Liquid
def self.call(template)
"ActionView::Template::Handlers::Liquid.new(self).render(#{template.source.inspect}, local_assigns)"
end
def initialize(view)
@view = view
end
def render(template, local_assigns = {})
@view.controller.headers["Content-Type"] ||= 'text/html; charset=utf-8'
assigns = @view.assigns
if @view.content_for?(:layout)
assigns["content_for_layout"] = @view.content_for(:layout)
end
assigns.merge!(local_assigns.stringify_keys)
controller = @view.controller
filters = if controller.respond_to?(:liquid_filters, true)
controller.send(:liquid_filters)
elsif controller.respond_to?(:master_helper_module)
[controller.master_helper_module]
else
[controller._helpers]
end
liquid = Liquid::Template.parse(template)
liquid.render(assigns, :filters => filters, :registers => {:action_view => @view, :controller => @view.controller})
end
def compilable?
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment