如何使用Liquid模板语言在布局内渲染模板? [英] How can you render a template within a layout using Liquid template language?
问题描述
我正在尝试在液体布局(液体模板lang,而不是CSS液体布局的东西)中渲染液体模板.我似乎无法获得布局部分以进行渲染.当前正在使用:
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
页面生成的HTML显示,模板"以液体的形式很好地呈现,但是没有用布局包裹(用呈现的模板替换布局中的"content_for_layout")
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)
推荐答案
只是让其他人知道谁遇到了这个问题,上面发布的代码确实有效,问题在于名为@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.
这篇关于如何使用Liquid模板语言在布局内渲染模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!