-
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 support #236
Comments
Hm I don't think we will implement this. Not a big demand for this at Shopify. Sorry. |
+1, Would be a big help for basic ternary operations
|
+1, I think this is needed. |
+1 must have,, gives the point of simplicity! |
+1 for this please |
This would definitely be a helpful feature. |
PS: "Not a big demand for this at Shopify" isn't the best answer for why a feature shouldn't exist in an open source project. :/ |
@Rican7 yeah right? That's why I decided to never use liquid again :D |
I don't work at Shopify and I don't speak for them, but I thought this was worth commenting on. Liquid is a Shopify project that they were kind enough to open source. If a request comes in for a feature that won't benefit Shopify and its customers, it makes sense that Shopify wouldn't spend resources implementing it and (from their perspective) adding bloat to the project. However, the beautify of open source is that you can fork the project and implement whatever features you want. For what it's worth, I agree with you that this would be a nice feature. Unfortunately it goes against Liquid's style of using just values, variables, and filters (and not expressions) for output, so I don't expect it to be implemented. For some of my projects, I add a custom module TernaryFilters
def if(value, true_output, untrue_output = '')
value ? true_output : untrue_output
end
def unless(value, untrue_output, true_output = '')
value ? true_output : untrue_output
end
end Try it in an irb session: # paste the module code from above
template = Liquid::Template.parse("{{ foo | if: 'yes', 'no' }}")
template.render({ 'foo' => true }, filters: TernaryFilters) # => "yes"
template.render({ 'foo' => false }, filters: TernaryFilters) # => "no" You of course may want to handle the value differently, such as treating a zero or an empty string as falsy. Hopefully this helps someone who wants a brief syntax for conditional output, Liquid-style. |
@Rican7: Maybe we have different understandings of what open source software means. To me, it means that we will publish the code that we use ourselves (!) so that others can benefit from it (for free). We are totally cool with you forking this project and implementing whatever features you need for yourself. We are happy to consider any suggestions that you make, but the final decision of whether we will merge it into our version of Liquid is up to us. You have no right at all to demand features being included and I think "we don't need this feature at Shopify" is a totally legit reason to reject things. If you don't like it, don't use it. Back to topic: I'm totally open to reconsider this decision. @Shopify/liquid, any more thoughts on the usefulness of this? @Shopify/design-gurus might have opinions too. Would this be helpful? |
@nickpearson makes a good point that Liquid folllows an approach of using just values, variables, and filters, rather than expressions, for output, however that can be a little constricting for many edge cases developers may encounter. Sure we can still achieve the desired output with a different set of variable assignments and conditions, but that, to us, is just a work around for not having an easy expression to call. So without asking to review Liquid's approach to have a higher focus on expressions, it would be useful to introduce some basic/fundamental expressions that have low complexity and introduce an easier process of spitting out information, ternary support being one. @fw42 Thanks for considering this as a possibility per discussion. |
I want to make it clear that I wasn't trying to insult anyone here. If I did, I apologize. I was simply trying to say that its a very closed off approach to just deny a feature and close the issue without further conversation from the community. The decision absolutely comes down to the owner of the project, and pandering to every community request isn't an option either, but its discouraging to see such a terse response on a feature request that may prove valuable to the community. Yes, the beauty of open source is that you're giving your code away for free, something that I believe anyone could appreciate. Its also under a very lenient license (MIT), so it could be modified and redistributed. However liquid has quite a large user base and usage pattern that isn't easily controlled by the user due to one fact: GitHub uses Jekyll, which uses Liquid templating, for both generating repo and user pages, so simply forking the project doesn't allow for the same workflow here. Either way, I'm happy to see the reconsideration here. Thanks for listening! 👍 |
In what we deal with day to day, I don't see a huge use for this… unless just to save a couple lines of code. I'd love to see a real world use case though. |
@fredryk Basic example.. {% if product.available %}
<link itemprop="availability" href="http://schema.org/InStock" />
{% else %}
<link itemprop="availability" href="http://schema.org/OutOfStock" />
{% endif %} versus <link itemprop="availability"
href="http://schema.org/{{ product.available ? 'InStock' : 'OutofStock' }}" /> Just one off the top of my head. It's pretty much a code saver, but it'd be a great-to-have 😄 Or conditional classes, it's much more conventional to have <div class="{{ product.available ? 'available' : 'outofstock' }}"> .. </div> versus {% if product.available %}
<div class="available">
{% else %}
<div class="unavaialble">
{% endif %}
.. content ..
</div> |
@ajwhite, we could simplify things a bit more already:
Can be shortened down:
I like the use case though. Thanks for posting it! :) |
@fredryk Ah yes, can I embarrassingly remove that comment now? 😉 Good point though, that isn't too ugly, but also slightly less friendly as a ternary. Works though |
Here is the use case:
Filling in by finding the first available:
|
Here's another one:
|
I find Ternary operator construct unnecessarily hard to read in Liquid. To me anyway, Liquid is not a programming language, it is meant to be super simple. I am all for making Liquid do things that were not possible before, when these things are most needed, but since Liquid can do in a more verbose ( in my opinion easier to read ) way what a Ternary operator would allow, then this would make me give a 👎 . |
Correct, but when you can make assignments, it would be nice to have decent support in that. Nothing crazy, but something like a ternary makes an assignment based on a condition a lot easier to write.
I disagree, using @TWiStErRob's example, i think
reads pretty easily. It feels a lot easier to read than
I guess that comes down to personal preference. I, personally, don't think multi-lined assignments due to verbosity is easier to read. Being used to ternaries, it feels like extra work to understand the assignment by having to read multiple lines of conditional reassignments. |
I think this is key here, I could also argue for |
Hmm, lookey here, it's not ternary, but at least Elvis is alive! (added in #267) |
Nice, even an "Elvis operator" is a nice step up. Would still be nice to have a true ternary, but this is a good find |
@fredryk you can simplify it even more:
|
This is a good reason why Shopify hasn't become the clear leader in ecommerce. They think supporting developers is not in their best interest. Unfortunately, they don't realize the power of a strong recommendation from a developer to a potential customer. |
Yes, because they do not implement every single requested feature in an excellent library they make freely available to everyone, Shopify hates developers. Sure, ternary operator support would be great, but your claim is unreasonable and way out of proportion. And if I remember correctly, they do accept pull requests. |
I just re-read my comment and it did sound a bit harsh. :( That said, the only reason I know Liquid is because they require it to build their themes. I'm a customer myself, and I've developed themes for many of their customers, so don't think I'm using a free library and then complaining. In addition, this is just one of the many examples where it is clear that shopify ignores requests even with a need. This isn't helping them become a clear leader. I've been developing ecommerce sites for almost a decade and continue to have a high hope that someone will emerge as the leader. I really thought Shopify would, and it has a lot going for it, but I can see they're missing the opportunity to cater to developers and thus grow their market share. |
Please reconsider reopening this issue as such a feature would be beneficial to some and [optional] to many as it wouldn't (re: shouldn't) break existing themes. |
Can anyone create a new issue for this? |
It appears there is another open issue about this feature request: #1264 |
+1 |
2 similar comments
+1 |
+1 |
+ထ |
Bruh, the ternary operators are literally some of the simplest things out there. And by making and developing Shopify themes, you'll have to eventually learn some Liquid template coding. You still have to learn for if/else statement for example: {% if product.available %}
<div class="available">
{% else %}
<div class="unavaialble">
{% endif %}
.. content ..
</div> and with the ternary operator you will end up only with: <div class="{{ product.available ? 'available' : 'outofstock' }}"> .. </div> which is much simpler and easier to read. By the way, it's 2023, still not added? |
I found this temp solution
and with the ternary operator you will end up only with:
By the way, it's 2023, still not added? |
10 years down the line and still not implemented. Why does Shopify provide such bad tools and support for developers? |
Another vote for this one |
+1 |
+1 Please |
+1 |
2 similar comments
+1 |
+1 |
+1 |
2 similar comments
+1 |
+1 |
Oh come on.. I got on this thread because I'd love a ternary operator and yes, 10 years after this has been open is a long time but...Shopify has really been great towards devs, especially in the past couple of years. I'd rather send them some love, maybe that's the way to get this topic sorted 😆 |
I must admit i am surprised this still hasnt been added. |
One century later.. |
Or... "one eternary later" 🤪. The most impressive comment is "Hm I don't think we will implement this. Not a big demand for this at Shopify. Sorry." (@fw42 ) |
That comment is over 10 years old. At the time it didn't seem like there was demand. Obviously I was wrong. Unfortunately I haven't worked for Shopify in over 4 years. |
@fw42 With your experience and approach, could you talk to the higher-ups, considering you're still in touch with them, and get them to notice this issue? |
Pleeeese add this feature! |
I am tired of writing I just want to do
|
Hello!
I think it would be awesome to implement ternary operator support like Twig does:
The text was updated successfully, but these errors were encountered: