如何使用 Liquid 模板语言在布局中渲染模板?

发布于 2024-08-02 04:16:49 字数 520 浏览 6 评论 0原文

我正在尝试在液体布局中渲染液体模板(液体模板语言,而不是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风疏影 2024-08-09 04:16:49

只是为了让其他人知道谁遇到了这个问题,上面发布的代码实际上确实有效,问题出在名为 @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.

弱骨蛰伏 2024-08-09 04:16:49

对于在 Rails 上的 ruby​​(尤其是 Rails 3)中使用液体 - 我相信渲染液体模板(并维护 Rails 为您所做的所有工作)的正确方法如下...

液体宝石本身提供了 Liquid_view Rails,以便您可以在调用 #render 时连接 Rails 以查找“液体”模板。 这个liquid_view仅适用于rails 2.3
但可以通过进行以下更新轻松更新以使用 Rails 3

if content_for_layout = @view.instance_variable_get("@content_for_layout")
  assigns['content_for_layout'] = content_for_layout
elsif @view.content_for?(:layout)
  assigns["content_for_layout"] = @view.content_for(:layout)
end
assigns.merge!(local_assigns.stringify_keys)

这可以在此处看到 --> https://github.com/danshultz/liquid/commit/e27b5fcd174f4b3916a73b9866e44ac0a012b1 82

然后正确渲染你的液体视图只需调用

render :template => "index", :layout => "my_layout", :locals => { liquid_drop1 => drop, liquid_drop2 => drop }

在我们的应用程序中,由于我们有一些常见的液体属性,我们已经覆盖了基本控制器中的“渲染”方法,以通过引用 #liquid_view_assigns 自动包含默认的局部变量,该方法会卷起额外添加的液滴进行渲染称呼

def render(...)
  options[:locals] = options.fetch(:locals, {}).merge(liquid_view_assigns)
  super
end

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

if content_for_layout = @view.instance_variable_get("@content_for_layout")
  assigns['content_for_layout'] = content_for_layout
elsif @view.content_for?(:layout)
  assigns["content_for_layout"] = @view.content_for(:layout)
end
assigns.merge!(local_assigns.stringify_keys)

This can be seen here --> https://github.com/danshultz/liquid/commit/e27b5fcd174f4b3916a73b9866e44ac0a012b182

Then to properly render your liquid view just call

render :template => "index", :layout => "my_layout", :locals => { liquid_drop1 => drop, liquid_drop2 => drop }

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

def render(...)
  options[:locals] = options.fetch(:locals, {}).merge(liquid_view_assigns)
  super
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文