Reputation: 4429
I'd like to use liquid in my Rails app. I've installed the gem. In order to use in all templates, I've created a library (lib/liquid_view.rb:
):
class LiquidView
def self.call(template)
"LiquidView.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
And added the following initialiser (config/initializers/liquid_template_handler.rb:
):
require 'liquid_view'
ActionView::Template.register_template_handler :liquid, LiquidView
PS: I've followed these instructions.
Now, if rename a template file with liquid my_template.html.liquid
the <%= stylesheet_link_tag 'mycss' %>
stopped working, but more importantly the {{user.first_name}}
variable did not print. In my controller I have @user = current_user
What am I missing?
My intention is to completely override erb
with liquid
in some templates, so ideally it should work like erb
(in a sense that I can pass variables from the controller and simply render it in the template without using Liquid::Template.parse(@page.template)
which by the way, I don't understand how it works on a file-based template.
PS: I'm also using [this] gem (https://github.com/yoolk/themes_on_rails) for separate templates. I'm not sure it does any impact on it.
PPS: I've seen this but doesn't apply as its a older version of Rails and I'm not using prepend.
PPPS: I'm using Ruby 2.2.2 and Rails 4.2
Upvotes: 1
Views: 1507
Reputation: 2999
Did you create a Drop to access @user?
https://github.com/Shopify/liquid/wiki/Introduction-to-Drops
Liquid is a safe template system, so we can interpret on the backend templates that are created by the user. To access anything non trivial (number, string, hashes or arrays) you need a Drop, which is a controlled interface to define what the templates can access.
This is by design and for security reasons.
Upvotes: 0
Reputation: 1085
I hope this not the problem you are thinking it is . You can check the way as it was said here Github Description
Upvotes: 1