Implement the snippet tag
#191
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR implements the
{% snippet %}tag, new in Shopify/liquid 5.10.0.TODO:
BoundTemplatefor 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, wheresnippet_nameis 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
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 %}