Skip to documentation content

Using Includes and Snippets

Using Includes and Snippets

Includes are developer-owned partial templates; snippets are editor-friendly HTML objects with no URL.

Breaking a page into smaller pieces keeps templates maintainable. Marketpath CMS gives you two different tools for that work: includes and snippets. They are not the same kind of object.

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 a content record an editor opens like a page. 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. Root entity is always set and reserved, so a page template does not guard it; if you pass it in as another name ({% include '/sections/item_content.liquid' item:entity %}), the partial should guard that input with {% if item.is_valid %}.

Snippets are a separate, editor-friendly object type. They are closer to articles or blog posts than to templates, except they have far fewer fields and they cannot have a URL. The main field on a snippet is HTML, and the CMS UI edits that field with a WYSIWYG editor. That makes snippets a good fit when an editor or non-technical marketer needs to change a reusable block of markup occasionally without looking at code or having access to other templates.

Output a snippet from Liquid with the snippet method, for example {% snippet output_to_html = "CTA1" %}, or by writing {{ snippet.content_html }}. Changing the snippet in the CMS updates every place that snippet is used. You can also insert a snippet into another HTML field from the editor toolbar; that inserts a live reference, not a one-time copy.

Choose an include when the fragment is structure or logic that belongs in a template. Choose a snippet when the fragment is content that editors should own. Keep each include or snippet focused on one job. For includes, 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. For the snippet object and methods, see the related Snippet reference.
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 is relative to the current template, and the absolute path is relative to 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 tag 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 -%}