Skip to content

Introduce Inline Snippets tag - #2001

Merged
karreiro merged 19 commits into
mainfrom
jb-inline-snippets
Oct 30, 2025
Merged

Introduce Inline Snippets tag#2001
karreiro merged 19 commits into
mainfrom
jb-inline-snippets

Conversation

@dejmedus

Copy link
Copy Markdown
Contributor

This PR implements RFC#1916 and introduces support for the new inline snippets {% snippet %} tag, which allows us to define reusable template components directly within Liquid files

Traditionally, snippets are stored as separate .liquid files in a snippets/ directory and rendered using the {% render %} tag. The {% snippet %} tag allows us to define and render inline snippets in the same file

  {% snippet input %}
    {% doc %}
      @param {string} type 
      @param {string} value
    {% enddoc %}

    <input type="{{ type }}" value="{{ value }}" />
  {% endsnippet %}

  {% render input, type: "text", value: "Hi!" %}

@karreiro karreiro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR, @dejmedus! Excellent stuff! I've left some minor comments, but it's really cool to see inline snippets working :) 🚀

Comment thread lib/liquid/tags/snippet.rb Outdated
Comment thread lib/liquid/locales/en.yml Outdated
Comment thread lib/liquid/tags/snippet.rb Outdated
Comment thread lib/liquid/tags/snippet.rb
Comment thread lib/liquid/tags/render.rb Outdated
Comment thread lib/liquid/tags/render.rb Outdated
Comment thread lib/liquid/tags/render.rb Outdated
Comment thread lib/liquid.rb Outdated
@karreiro
karreiro requested a review from charlespwd October 21, 2025 16:51
Comment thread lib/liquid/tags/render.rb Outdated
@dejmedus
dejmedus force-pushed the jb-inline-snippets branch 3 times, most recently from bdf3e2a to 266e1ed Compare October 22, 2025 22:47

@Maaarcocr Maaarcocr left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given that we are allowing variables to be passed to render like we do for blocks in shopify themes, I think it would be wise to have a shared interface here that we can use in both places.

what does this mean practically? instead of checking for SnippetDrop, if the ruby object that is passed responds to a certain method (let's say render or maybe a less common name that may not be mistakenly implemented by other drops) then it will get called.

This way we can remove our monkey patching of render in storefronts, as having callable objects is now a real liquid feature and it has a shared interface we can use.

@dejmedus

dejmedus commented Oct 24, 2025

Copy link
Copy Markdown
Contributor Author

instead of checking for SnippetDrop, if the ruby object that is passed responds to a certain method

Thank you for the detailed explanation @Maaarcocr! I added a to_partial method to SnippetDrop so render can differentiate by respond_to?(:to_partial). Does this sound like what we are aiming for?

@dejmedus
dejmedus marked this pull request as ready for review October 24, 2025 00:15
Comment thread lib/liquid/tags/snippet.rb Outdated
Base automatically changed from rigid-mode to main October 27, 2025 15:56
EvilGenius13 and others added 15 commits October 27, 2025 10:11
Inline snippets will reduce code duplication and
improve the developer experience, eliminating the
need for one-off snippet files
Previously, inline snippets syntax looked a bit
different, they:

- used strings as tag identifiers
- defined tag arguments {% snippet "input" |type| %}

This PR updates snippets to better reflect
the currently proposed syntax

Co-authored-by: Orlando Qiu <orlando.qiu@shopify.com>
Currently, snippet files identified by strings. This
PR makes changes to render to allow for new inline
snippets to use variables as identifiers instead
This commit updates the render method to share parts
of the snippet and block rendering logic to enable
inline snippets to support `with`, `for`, and `as`
syntax

@karreiro karreiro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this PR, @dejmedus! I just left one minor question left, but overall, everything looks good to me :)

Comment thread lib/liquid/tags/render.rb Outdated
@dejmedus
dejmedus requested a review from Maaarcocr October 27, 2025 18:52
Comment thread lib/liquid/tags/snippet.rb Outdated
Comment thread lib/liquid/tags/snippet.rb

@karreiro karreiro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, @dejmedus! Excellent stuff 🚀

@dejmedus
dejmedus requested a review from charlespwd October 28, 2025 17:47
@Maaarcocr

Maaarcocr commented Oct 29, 2025

Copy link
Copy Markdown
Contributor

looks solid. one suggestion, if we do not have a test for this, can you please add a test for something like:

{% snippet foo %}
{% render "THIS_DOES_NOT_EXIST" %}
{% endsnippet %}

{% render foo %}

to see what file and line number the render for rendering a non existing snippet has?
(the render inside the snippet is just a simple way to generate a liquid error)

in theory I would expect this to render something like:

Liquid error (<filename> line 2): the snippet cannot be found or something like this

but what I care about is <filename>. if that code is contained in a file called bar.liquid <filename> should be bar

Comment thread lib/liquid/tags/render.rb Outdated
Comment thread test/integration/tags/snippet_test.rb Outdated
@karreiro
karreiro requested a review from Maaarcocr October 30, 2025 08:34

@karreiro karreiro left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the changes, @dejmedus! Excellent catches, @Maaarcocr!

Comment thread lib/liquid/tags/render.rb Outdated
@karreiro
karreiro merged commit a16ec56 into main Oct 30, 2025
23 checks passed
@bakura10

Copy link
Copy Markdown
Contributor

Awesome feature! Do we know when this will be added to Shopify? :)

Comment thread lib/liquid/tags/render.rb
partial = template.to_partial
template_name = template.filename
context_variable_name = @alias_name || template.name
elsif @template_name_expr.is_a?(String)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we should first check is_a?(String) since its the common/hot path

Comment thread lib/liquid/tags/render.rb

if template.respond_to?(:to_partial)
partial = template.to_partial
template_name = template.filename

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be good to respond to name as well for duck typing

private

def assign_score_of(snippet_drop)
snippet_drop.body.nodelist.sum { |node| node.to_s.bytesize }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this makes sense. Assigning a snippet drop shouldn't rely on parsed AST for this functionality.

karreiro added a commit that referenced this pull request Nov 19, 2025
Liquid to 5.11. For now, the inclusion of Inline Snippets
in the latest Liquid release is being treated as a bug.

While #2001 does implement the scope contained in RFC#1916,
we need to take a step back to make sure we’re setting our
sights high enough with this feature, and that we’re truly
supporting theme developers in the ways they need.

If you have any feedback, please leave a comment on RFC#1916.

- Liquid Developer Tools
@karreiro karreiro mentioned this pull request Nov 19, 2025
karreiro added a commit that referenced this pull request Nov 19, 2025
This commit reverts the Inline Snippets tag (#2001) and bumps
Liquid to 5.11. For now, the inclusion of Inline Snippets
in the latest Liquid release is being treated as a bug.

While #2001 does implement the scope contained in RFC#1916,
we need to take a step back to make sure we’re setting our
sights high enough with this feature, and that we’re truly
supporting theme developers in the ways they need.

If you have any feedback, please leave a comment on RFC#1916.

- Liquid Developer Tools
@PaulNewton

Copy link
Copy Markdown

If this is being reverted good

https://github.com/Shopify/liquid/pull/2017/files#diff-abfa5988643af1b8b2600aac13b273922dbb3372e021bf1d7caadfe7473c9561R37

Please consider renaming this so we are not overloading even more concepts words in liquid and downstream platforms
.With this out in it's current naming it adds a permanent type of friction everywhere i.e. when someone says "fix the snippet bug" what are they talking about a file or a tag in some other file.

Wrote about the pedagogy of this in the RFC #1916 (comment)

@bakura10

Copy link
Copy Markdown
Contributor

@karreiro any info on when this will be merged to Shopify? We have a few use cases in our code where this would drastically remove duplication.

@PaulNewton

Copy link
Copy Markdown

@bakura10 it's reverted, and is in RFC #1916
I can use this like crazy too but it's current approach is flawed and needs more scrutiny and better feedback; and non-ambiguous naming or it's just gonna confuse merchants.

Probably be faster if the slack was still around so more shopify-partner even knew this was a thing they were finally chasing so more people were chiming in, or pushing on their shopify-connections.

v5.11.0 Latest
What's Changed
...
Revert the Inline Snippets tag (https://github.com/Shopify/liquid/pull/2001), treat its inclusion in the latest Liquid release as a bug, and allow for feedback on RFC#1916 to better support Liquid developers by @karreiro in https://github.com/Shopify/liquid/pull/2012

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.

8 participants