Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you render a template within a layout using Liquid template language?

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)

like image 240
bwizzy Avatar asked Aug 15 '09 23:08

bwizzy


People also ask

How do I render a template in Shopify?

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.

What is Liquid templating language?

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.

What is render in Shopify?

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.

How does Liquid work Shopify?

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.


1 Answers

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.

like image 166
bwizzy Avatar answered Oct 17 '22 01:10

bwizzy