-
Couldn't load subscription status.
- Fork 1.5k
Introduce Inline Snippets tag #2001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
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 :) 🚀
bdf3e2a to
266e1ed
Compare
There was a problem hiding this comment.
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.
c0918c5 to
1133386
Compare
Thank you for the detailed explanation @Maaarcocr! I added a |
lib/liquid/tags/snippet.rb
Outdated
| # {% snippet input %} | ||
| # value | ||
| # {% endsnippet %} | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could remove this line:
(I'm almost sure it doesn't impact YARD pipeline, but I think it would be nice to remove to be 100% sure and also be cohesive with the other tags) 😌
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call! Fixed
52ae826 to
fecdb2c
Compare
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
fecdb2c to
47288cc
Compare
There was a problem hiding this comment.
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 :)
lib/liquid/tags/snippet.rb
Outdated
| # @liquid_description | ||
| # You can create inline snippets to make your Liquid code more modular. | ||
| # @liquid_syntax | ||
| # {% snippet input %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non blocking: we should name this snippet_name instead? So that we'd call {% render snippet_name %}?
Looks like the docs for assign are using variable_name. We can try to be consistent here.
| @to = p.consume(:id) | ||
| else | ||
| raise SyntaxError, options[:locale].t("errors.syntax.snippet") | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're missing a p.consume(:end_of_text), otherwise folks could do stuff like {% snippet foo bar baz %} and we wouldn't be throwing an error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Fixed
There was a problem hiding this comment.
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 🚀
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 filesTraditionally, snippets are stored as separate
.liquidfiles in asnippets/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!" %}