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)
A template can be rendered once for each value of an enumerable object by using the for and optional as parameters. In the example above, the template will be rendered once for each variant of the product, and the variant variable will hold a different product variant object for each iteration.
Liquid is a template language that allows us to display data in a template. Liquid has constructs such as output, logic, loops and deals with variables. Liquid files are a mixture of HTML and Liquid code, and have the . liquid file extension.
The render tag isolates the variables that are used in the snippet that is being rendered. Using the render tag increases the performance of the storefront on themes and makes theme code easier to understand and maintain.
The Liquid code is essentially a placeholder. It's used to retrieve specific referenced data from your Shopify store—like your store name, product details, images, etc. —when the code is compiled and sent to the browser. Then, the browser grabs assets like your Javascript and CSS files to display your customized theme.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With