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.

Revisions

  1. greendog created this gist Jan 23, 2013.
    36 changes: 36 additions & 0 deletions liquid.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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