[英]How to replace erb with liquid?
I'd like to use liquid in my Rails app. 我想在Rails应用程序中使用液体。 I've installed the gem.
我已经安装了宝石。 In order to use in all templates, I've created a library (
lib/liquid_view.rb:
): 为了在所有模板中使用,我创建了一个库(
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:
): 并添加了以下初始化程序(
config/initializers/liquid_template_handler.rb:
:
require 'liquid_view'
ActionView::Template.register_template_handler :liquid, LiquidView
PS: I've followed these instructions. PS:我已经按照这些指示进行了。
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. 现在,如果使用液体
my_template.html.liquid
重命名模板文件,则<%= stylesheet_link_tag 'mycss' %>
停止工作,但更重要的是{{user.first_name}}
变量不会显示。 In my controller I have @user = current_user
在我的控制器中,我有
@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. 我的意图是在某些模板中用
liquid
完全覆盖erb
,因此理想情况下它应该像erb
一样工作(在某种意义上,我可以从控制器传递变量并将其简单地呈现在模板中,而无需使用Liquid::Template.parse(@page.template)
,但我不了解它如何在基于文件的模板上工作。
PS: I'm also using [this] gem ( https://github.com/yoolk/themes_on_rails ) for separate templates. PS:我还将[this] gem( https://github.com/yoolk/themes_on_rails )用于单独的模板。 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. PPS:我已经看到了这一点,但由于它是旧版本的Rails,因此并不适用,并且我没有使用prepend。
PPPS: I'm using Ruby 2.2.2 and Rails 4.2 PPPS:我正在使用Ruby 2.2.2和Rails 4.2
I hope this not the problem you are thinking it is . 我希望这不是您想的问题。 You can check the way as it was said here Github Description
您可以按照这里所说的方法检查Github描述
Did you create a Drop to access @user? 您是否创建了一个Drop来访问@user?
https://github.com/Shopify/liquid/wiki/Introduction-to-Drops 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. Liquid是一个安全的模板系统,因此我们可以解释用户创建的后端模板。 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.
要访问任何非平凡的东西(数字,字符串,哈希或数组),您需要一个Drop,这是一个受控接口,用于定义模板可以访问的内容。
This is by design and for security reasons. 这是设计使然且出于安全原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.