Inline Snippets #1916
karreiro
started this conversation in
Requests for Suggestions
Inline Snippets
#1916
Replies: 1 comment
-
Array of argumentsI like the idea of exposing render tag arguments as an array inside snippets, a bit like Jinja does with call/macro. If we were to call said array {% snippet tabs %}
{% snippet tab %}
<li>
<a href="#tab-{{ name }}">
Tab {{ value }}
</a>
</li>
{% endsnippet %}
<div>
<ul>
{% for argument in arguments %}
{% render tab, name: argument[0], value: argument[1] %}
{% endfor %}
</ul>
</div>
{% endsnippet %}
{% render "tabs", blog: 'some blog', page: 'some page' %} There is a risk that Changing the render tag to accept positional arguments as well as named/keyword arguments might be useful too, but then we'd need to reserve names for two arrays, like |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Author: Shopify
Expected end date: March 21st, 2025
Background
Liquid templates can often become quite verbose, with snippets serving as the primary method for reusing code.
Developers frequently need to choose between creating many separate files in a "snippets" directory or managing large and complex Liquid templates. This dilemma can lead to complicated templates and duplication.
For example, when working on a
product.liquid
section, developers frequently break it down into:snippets/product-header.liquid
snippets/product-accordion.liquid
snippets/product-content.liquid
snippets/product-media-modal.liquid
snippets/product-media.liquid
snippets/product-thumbnail.liquid
This approach can clutter the snippets directory with files that are only relevant to a specific template, making the codebase less cohesive, harder to maintain, and to organize, especially as it grows.
Proposal
We propose introducing inline snippets—a feature that allows developers to define and reuse code within the same file. This eliminates the need for additional snippet files when the code is only relevant within a specific context.
Consider the following
snippets/nav.liquid
file:Nested inline snippets
Inline snippets can also be nested and accessed externally, allowing developers to group related snippets into cohesive modules. Here's the updated
snippets/nav.liquid
file:The
header
,tabs
, andtab
snippets are defined within the file. Notice that thetab
snippet is accessed using dot notation since it's nested withintabs
:The
snippets/nav.liquid
file can still be rendered entirely or have only a specific snippet within it rendered (for example,"nav.header"
):By grouping related snippets under a common namespace, developers can organize related snippets together and reduce the number of files.
Call for suggestions
We welcome any feedback or opinions on this proposal. Please share your thoughts by March 21st, 2025. Your input is valuable as we prepare to begin active development on this initiative.
Beta Was this translation helpful? Give feedback.
All reactions