如何使用 Liquid 模板语言在布局中渲染模板?
我正在尝试在液体布局中渲染液体模板(液体模板语言,而不是 CSS 液体布局内容)。 我似乎无法渲染布局部分。 当前使用:
assigns = {'page_name' => 'test'}
@layout = Liquid::Template.parse(File.new(@theme.layout.path).read)
@template = Liquid::Template.parse(File.new(self.template.path).read)
@rend_temp = @template.render(assigns)
@rend_layout = @layout.render({'content_for_layout' => @rend_temp})
render :text => @rend_layout, :content_type => :html
页面的 HTML 显示“模板”在液体中渲染得很好,但它没有被布局包裹(用渲染的模板替换布局中的“content_for_layout”)
I'm trying to render a liquid template within a liquid layout (Liquid Template lang, not CSS liquid layout stuff). I can't seem to get the layout part to render. Currently using:
assigns = {'page_name' => 'test'}
@layout = Liquid::Template.parse(File.new(@theme.layout.path).read)
@template = Liquid::Template.parse(File.new(self.template.path).read)
@rend_temp = @template.render(assigns)
@rend_layout = @layout.render({'content_for_layout' => @rend_temp})
render :text => @rend_layout, :content_type => :html
The resulting HTML of the page shows that the 'template' rendered in liquid fine, but it isn't wrapped with the layout (replacing 'content_for_layout' in the layout with the rendered template)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是为了让其他人知道谁遇到了这个问题,上面发布的代码实际上确实有效,问题出在名为 @template 的变量上。 我将@template和@layout重命名为@_tempalte和@_layout,一切都按预期工作。
Just to let anyone else know who comes across this problem, the code posted above actually does work, the issue is with the variable named @template. I renamed @template, and @layout to @_tempalte, and @_layout and everything works as expected.
对于在 Rails 上的 ruby(尤其是 Rails 3)中使用液体 - 我相信渲染液体模板(并维护 Rails 为您所做的所有工作)的正确方法如下...
液体宝石本身提供了 Liquid_view Rails,以便您可以在调用 #render 时连接 Rails 以查找“液体”模板。 这个liquid_view仅适用于rails 2.3
但可以通过进行以下更新轻松更新以使用 Rails 3
这可以在此处看到 --> https://github.com/danshultz/liquid/commit/e27b5fcd174f4b3916a73b9866e44ac0a012b1 82
然后正确渲染你的液体视图只需调用
在我们的应用程序中,由于我们有一些常见的液体属性,我们已经覆盖了基本控制器中的“渲染”方法,以通过引用 #liquid_view_assigns 自动包含默认的局部变量,该方法会卷起额外添加的液滴进行渲染称呼
For using liquid in ruby on rails (especially rails 3) - I believe the proper way to render your liquid templates (and also maintain all the work rails is doing for you) is as follows...
The liquid gem itself provides a liquid_view for rails so you can wire up the rails to look for "liquid" templates when you call #render. This liquid_view only works fully with rails 2.3
but can easily be updated to work with rails 3 by making the following update
This can be seen here --> https://github.com/danshultz/liquid/commit/e27b5fcd174f4b3916a73b9866e44ac0a012b182
Then to properly render your liquid view just call
In our application, since we have a handful of common liquid attributes we have overriden the "render" method in our base controller to automatically include the default locals by referencing #liquid_view_assigns which roll up additionally added liquid drops for the render call