Skip to documentation content

Includes

Includes

An include renders another template in a child scope: what it inherits, how to pass data in, and how its path is resolved.

The include method renders another template from the current one. Templates are developer-focused and code-first: you edit Liquid in the template editor, not in a content record an editor opens like a page. A snippet is a different kind of object; see Snippets, and Reusing Content for choosing between them.

The included template runs in a new child scope. Variables you create inside it (for example with {% var %}) do not leak back into the parent, and when the include finishes the parent scope is unchanged. You can pass variables into the included template so it has the data it needs. Use includes for repeated layout fragments that should stay self-contained, such as a header, a card, or a form field group.

If you pass the root entity in under another name ({% include '/sections/item_content.liquid' item:entity %}), the partial should guard that input with {% if item.is_valid %}; The Page and Its Inputs explains why.

Paths in an include. The text after include is a path, and it can be absolute or relative. An absolute path begins with / and is taken from the root, so it means the same wherever the including template sits. A relative path is resolved against the folder of the template being rendered, and .. moves up one folder; inside an included partial, relative means relative to that partial's own folder. ./ is not supported, so write search-result.liquid, not ./search-result.liquid. A relative path depends on where the including template sits and an absolute path depends only on where the target sits, so which one reads better or survives a reorganization depends on how the site is laid out; neither suits every site, and mixing them is fine. The name has to match the stored name, extension included, because the CMS adds nothing and strips nothing. {% template %}, {% stylesheet %}, {% javascript %}, {% add_stylesheet %} and {% add_javascript %} resolve a path the same way. The list methods {% templates %}, {% stylesheets %} and {% javascripts %} do not: they read every path from the root, so partials/_card.liquid is relative to the current folder in an include but means /partials/_card.liquid in a list method argument. See the include method for the full rules.

Keep each include focused on one job. Pass in only what the partial needs, and avoid relying on parent-scope variables unless you pass them explicitly, so the partial stays reusable. For examples of dynamic includes and passing markup into an included template, see the examples on this page.
Example How to use the {% include %} method

Basic use case

Liquid
{% include "/_header.liquid" %}

Includes the partial template from the specified path. This is the simplest way to include a partial template.

Include partial by string with attributes

Liquid
{% include "/sections/_banner.liquid" section_class:'banner-section mb-5' show_title:true show_description:false %}

Includes the partial template from the specified path. The new scope created by the include method will have the specified attributes available as variables.

Expanded example with relative and absolute paths

Liquid
{%- search searchCollection "keyword" limit:10 page:search-page -%}
{%- for result in searchCollection -%}
	{%- include "search-result.liquid" item:result -%}
{%- endfor -%}
{%- include "/shared/_pagination.liquid" collection:searchCollection style:"links" max_links:5 -%}

Includes the partial template from the specified path. The relative path, search-result.liquid, is resolved against the folder of the template doing the including; do not start it with ./, which is not supported. The absolute path begins with / and is taken from the root of the website.

Example Include a template whose name is in a variableDemonstrates multple ways to dynamically include a template. Note that in all of these examples, the template fields will NOT be compiled in the page definition.

Dynamically Include a Template from a Variable

Liquid
{%- var dynamicTemplate = "/partials/footer-main.liquid" -%}
{%- include dynamicTemplate -%}

Dynamically Include Template from a Select List

Liquid
{%- if page.page_layout.is_valid -%}
	<div class="sidebar-{{ page.page_layout.value }}">
		{%- var includeTemplate = "/theme/sidebar/" | append:page.page_layout.value -%}
		{%- include includeTemplate -%}
	</div>
{%- endif -%}

Assuming that page_layout is a select field or similar, this code dynamically creates the name of the template to include based on the selected value.

Dynamically Include Multiple Templates from a TemplateList Field

Liquid
{%- if page.sidebar_sections.count > 0 -%}
	{%- for section in page.sidebar_sections.selected -%}
		{%- include section -%}
	{%- endfor -%}
{%- endif -%}

Assuming that sidebar_sections is a templatelist field or similar, this code dynamically includes each of the selected templates.

Check if a dynamic template exists before including it

Liquid
{%- template var dynamicTemplate = "/theme/sidebar/" | append: page.sidebar_section.value | append: ".liquid" -%}
{%- if dynamicTemplate.is_valid -%}
	{%- include dynamicTemplate -%}
{%- endif -%}

Assuming that sidebar_sections is a templatelist field or similar, this code dynamically includes each of the selected templates.

Example Checkbox Include Partial TemplateInclude a partial template conditionally when a checkbox (or other) field is checked.
Liquid
<p>Show Sidebar? <strong>{{ page.show_sidebar }}</strong></p>
{%- if page.show_sidebar.checked -%}
	{%- include "Sidebar" -%}
{%- endif -%}
Example Capturing markup and passing it to an included templateCapture a block of markup with the capture method and pass it to an included template as a variable.
Liquid
{%- capture blockContent -%}
	<div class="block-outer">
		<h3 class="block-title">
			{%- if article.default_page_url.is_valid -%}
				<a href="{{article.default_page_url.value}}">{{article.title}}</a>
			{%- else -%}
				{{-article.title-}}
			{%- endif -%}
		</h3>
		<div class="block-description">{{article.summary_html}}</div>
		<div class="block-footer">Posted {{article.post_date}}</div>
	</div>
{%- endcapture -%}
{%- include "_block_outer" content:blockContent -%}