Liquid Tags: Include

Last updated on June 18, 2024

The tag allows you to make your code more maintainable by including templates within other templates.

The simplest example of looks like this:

app/views/pages/include.liquid

app/views/partials/mypartial.liquid

app/views/partials/mypartial2.liquid

After expanding , rendered content becomes:

You can include the exact partial multiple times in multiple places for code reuse.

This article details the following topics:

  • Passing parameters to a partial
  • Local variable using with
  • Iterating over a collection using for
  • Using and exporting private variables

Passing Parameters to a Partial

You can pass any number of parameters to a partial.

app/views/pages/index.liquid

app/views/partials/car.liquid

Tip

Make sure you put space between the parameter name and its value. will not work.

Important

You cannot name a parameter the same as the partial (in this case ).

Local Variable using 'with'

Liquid has some issues when using a variable named the same as the partial. For example, if you have this partial:

You won't be able to set the variable by passing it during the include:

but you will be able to set this variable by using :

This will create the variable called with hash inside of it.

The properties of hash are accessible using the syntax, for example:

Iterating over a Collection Using 'for'

This will iterate over the collection and render the partial for each item. Each iteration will have the variable populated with the current item.

A partial that looks like this:

app/views/partials/product.liquid

Will render (whitespace has been changed for readability):

Using and Exporting Private Variables

Our Liquid implementation is 99% compatible with the official one, but we couldn't ignore the issue that you can only use global variables in Liquid.

In Insites, when you define a variable in a partial, it is not visible by default on a page including that partial.

It works from top to bottom, though: If you define a variable on a page and then include it in a partial, it will be accessible in the partial.

That means that given this partial:

app/views/partials/variable.liquid

And this page using it:

app/views/pages/include.liquid

Will not return 'Honda' on the page. The variable can be accessed only inside the partial.

To use a variable that has been defined inside a partial, you need to use the tag and outside of it.

app/views/partials/export.liquid

app/views/pages/include.liquid

This should render the stringified hash stored inside the variable in the first line and nested key in the second:

Have a suggestion for this page?

Didn't quite find what you are looking for or have feedback on how we can make the content better then we would love to hear from you. Please provide us feedback and we will get back to you shortly.