Skip to content

Conversation

@jg-rp
Copy link
Owner

@jg-rp jg-rp commented Oct 31, 2025

This PR implements the {% snippet %} tag, new in Shopify/liquid 5.10.0.

TODO:

  • Subclass BoundTemplate for inline instances so we get a reasonable string representation when output.

Inline snippets

A snippet is a reusable block of Liquid markup. Traditionally we'd save a snippet to a file and include it in a template with the {% render %} tag.

{% render  "some_snippet" %}

Now, with the {% snippet %} tag, we can define blocks for reuse inside a single template, potentially reducing the number of snippet files we need.

{% snippet div %}
  <div>
    {{ content }}
  </div>
{% endsnippet %}

Defining a snippet does not render it. We use {% render snippet_name %} to render a snippet, where snippet_name is the name of your snippet without quotes (file-based snippet names must be quoted).

{% snippet div %}
  <div>
    {{ content }}
  </div>
{% endsnippet %}

{% render div, content: "Some content" %}
{% render div, content: "Other content" %}

Output

  <div>
    Some content
  </div>


  <div>
    Other content
  </div>

Inline snippets share the same namespace as variables defined with {% assign %} and {% capture %}, so be wary of accidentally overwriting snippets with non-snippet data.

{% snippet foo %}Hello{% endsnippet %}
{% foo = 42 %}
{% render foo %} {% # error %}

Snippets can be nested and follow the same scoping rules as file-based snippets.

{% snippet a %}
  b
  {% snippet c %}
    d
  {% endsnippet %}
  {% render c %}
{% endsnippet %}

{% render a %}
{% render c %} {% # error, c is out of scope %}

Snippet blocks are bound to their names late. You can conditionally define multiple snippets with the same name and pick one at render time.

{% if x %}
  {% snippet a %}b{% endsnippet %}
{% else %}
  {% snippet a %}c{% endsnippet %}
{% endif %}
{% render a %}

@jg-rp jg-rp marked this pull request as ready for review November 1, 2025 09:01
@jg-rp jg-rp merged commit 4b3d5ad into main Nov 7, 2025
30 checks passed
@jg-rp jg-rp deleted the snippet branch November 7, 2025 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants