Template Handler for Rails 3.1

322 views
Skip to first unread message

Sébastien Gruhier

unread,
Sep 21, 2011, 3:09:16 AM9/21/11
to liquid-t...@googlegroups.com
Hi 

I need to use liquid templating in a Rails 3.1 application. I didn't find any code about porting extra/liquid_view.rb to rails 3.1
So I did this
https://gist.github.com/1231441

Please add any comments to this gist, I'm not sure it's the best way to do it 
Thanks
Seb

Masiha H

unread,
Aug 27, 2012, 3:24:03 PM8/27/12
to liquid-t...@googlegroups.com
You can use this handler in your initializer

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

Masiha H

unread,
Aug 27, 2012, 3:24:38 PM8/27/12
to liquid-t...@googlegroups.com
and this too

require 'action_view/template/handlers/liquid'
ActionView::Template.register_template_handler :liquid,
ActionView::Template::Handlers::Liquid         

On Wednesday, September 21, 2011 12:09:16 AM UTC-7, Sébastien Gruhier wrote:
Reply all
Reply to author
Forward
0 new messages