{% capture %}

Captures the string inside of the opening and closing tags and assigns it to a variable. Capture makes it easy to create complex strings using a variety of methods.

Syntax

{% capture [var|set|assign] name %}code block{% endcapture %}

If you do not specify one of "var", "set", or "assign", then the "var" behavior will be used - which is to save the result on the current scope.

The capture tag creates a new child scope.

If name starts with '&', will attempt to assign the value by reference.

Examples

Using Capture to create page descriptions

Copy
{% capture page_description -%} {{ page.browser_title }} | {{ site.name }}: {% unless page.browser_title contains entity.title %} [{{ entity.title }}] {% endunless %} {{ page.meta_description | truncate: 50 '...' }} {%- endcapture %} The page description is "{{ page_description | escape }}"

Capturing markup and passing it to an included template

Copy
{% capture blockContent %}

{% if article.default_page_url.is_valid %} {{article.title}} {% else %} {{article.title}} {% endif %}

{{article.summary_html}}
{% endcapture %} {% include "_block_outer" content:blockContent %}