-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Ternary operator feature support #1264
Comments
This or #1271 would be so clean! 🚀 |
Please implement this. Saving heaps of lines of unnecessary code. |
This would be really handy and add readability to the code. I would love to see Liquid getting closer to JavaScript syntax (Ruby here also). |
It seems. like the difference between what you want and the default filter is the ability to specify the value to use when it is truthy. The Starting with a filter would make it much easier to introduce without worrying about backwards compatibility concerns. It wouldn't preclude the introduction of special syntax for it. It would actually make it easier to gauge adoption of the feature, that would help with justifying special syntax for making it more concise. |
@dylanahsmith Would love to see an example of suggested filter option. Either way it would be an awesome feature, I think You thinking of something like this, or what is the idea of replacing ?: with filter names? |
I think this feature will take another decade to be considered by the staff, lol |
I was thinking it would be a single filter. We already have a
I'm just not sure about using the word "truthy" in the filter name. That is kind of a non-standard word that seems to be introduced to describe the way programming/template languages handle Are there object-oriented programming languages that try to provide something like "if" as a method that we could take inspiration from? E.g. Possibly the awkwardness of the naming is why the C ternary syntax uses |
How about just
Personally, I like the one where |
I already tried to explain why just an
E.g. {% liquid
if b
assign a
else
assign c
endif
%} especially to python programmers, since python has the |
+1 |
1 similar comment
+1 |
@dylanahsmith How about something like this, that would remove the confusion of the wording, even with the if/unless keywords. As it reads how you would return it {%- liquid
assign a = 'a' | if: 'something true', then: 'b'
assign b = 'b' | unless: 'something true', then: 'a'
-%} Or a combined wording, does seem more messy though {%- liquid
assign a = 'a' | ifthen: 'something true', 'b'
assign b = 'b' | unlessthen: 'something true', 'a'
-%} |
Oh, I had kept thinking that we would want the condition evaluated first, so having it as the filter input would make sense. However, it isn't like a filter is going to short circuit the evaluation of arguments anyways and the order of evaluation generally shouldn't matter in liquid, with avoiding liquid errors on the unused branch being a notable exception. If we really want short circuit evaluation, then that might be a good argument in favour of dedicated syntax. If we do go with a filter, then having the condition as the first argument to the filter does allow it to be readable as well as consistent with python's ternary operation. In that case, I think we would want That seems fine at first, until realizing that filter argument expressions aren't consistent with condition expressions, so the It looks like we really do need to evolve the language itself, so that it has first class expression support, in which we could provide an actual ternary operator. E.g. we would ideally be able to actually specify something like a python ternary operator |
@dylanahsmith Appreciate the detailed response on this! |
GPT4 totally told me I could use ternaries in liquid. It was upset with itself when I told it the bad news.
|
+1 |
ChatGPT has a 100% failure rate when I've asked it for any Shopify Liquid or API help. Gets it wrong on the first attempt, and then either repeats the same mistake over and over, or hallucinates an answer to 'please' me 🥴 ...anyway: +1 to ternary operators! |
+1 for ternaries! I also was bamboozled by ChatGPT's sweet little lies... |
@dylanahsmith You still the man for this feature request? Or who do you reference nowadays on stuff like this? maybe @karreiro have anything to add? 😊 |
I know this feature has been requested before in this thread: #236
And based on 85 liking the idea, versus 2 not liking the idea says something.
But that issue was closed back in 2016, and as the docs says:
I personally still see a great advantage in having the opportunity to use ternary operators, the code is way more slick and friendly to look at.
I saw the
default
filter was suggested, but doesn't always come in handy, when using logic.Without ternary
With ternary
The text was updated successfully, but these errors were encountered: