-
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
How to express logic "and","or", "not"? #138
Comments
To work around this, I've always used nested So, if you have this in a language that has a if a && b && !c
# ...
end then you can do this in Liquid:
It can get tricky if you need complex |
@dapengme what is your specific usecase? you can also use |
Since this is not really an issue but more of a question, and it's pretty old, I will close this for now. Please re-open if necessary. |
Without "not", it is quite awkward to express something like "if not p.url contains '/'" |
|
That works only for simple, not combined conditions. I've worked around it, but "not" is a legitimate boolop that is not entirely replacable by unless. |
Sometimes it would make a lot more sense to have things that behave like "does not contain", rather than an "unless" statement. It would keep things cleaner and offer more options. |
I agree with Cam and AllThatIsTheCase. It is difficult to check if, for example, a collection of product.tags and customer.tags do not contain the tag "wholesale" without a "not contains" operator because of the combined condition. |
Reopened because of #526 |
From #526:
Ah. Sounds like the problem is the parser support for not, because you're not using a parser generator or something like that — and you can't parse the context free grammar of expressions with parentheses with regular expressions such as Line 14 in 12d526a
|
We could totally implement parentheses with the new parsers, but it would probably break some templates that are using them regardless of whether they do anything or not. We even have a test asserting that parentheses are broken :( If this feature is wanted, though, I would be happy to do the necessary work, which would include killing certain portions of the lax parser (which we want to do eventually anyway, right?) |
The other option is to have some kind of super-conditional tag or syntax that is only available with liquid-c or the strict ruby parser. That way it could be opt-in and not break existing templates. Maybe call it Or it could be an opt in for expressions. Maybe some special kind of augmented parentheses in which real expression syntax is valid. Maybe something like the magic test command in bash: |
That would work, but it would also be really confusing going forward. I think if we do this at all we should work towards unifying syntax and formally specifying it. |
As a programmer-minded user, I'd be happy to set a flag to enable an extended syntax, where my One could also argue that flag should be orthogonal to strict vs lax mode. In fact, when transitioning, I'll probably need to switch to strict mode to check our templates actually parse (i.e., probably, they should contain no parentheses), before enabling a new syntax where these parenthesis suddenly can be used and have meaning. |
+1 for the formal specification. Right now there's some divergence among various flavours, e.g. the "keyword" style of Jekyll's parameters in |
I'm afraid neither Shopify nor Jekyll can make any drastic changes now since a huge number of users already depends on what we/they have :-) |
@fw42 Yeah, I'm thinking more of myself here Re: specs---I'm porting liquid to another environment and was hoping not to accidentally end up in the same position. :) |
It's a shame this is still an issue. |
@Tamriel: Patches welcome |
In this case it's not simple at all and any patch we accept is going to take a long time and a lot of testing/validation on our end. |
You're really concerned that it would "break some weird-ass templates that are using them regardless of whether they do anything or not"? I mean, correct me if I'm wrong but parentheses currently don't do anything, right? |
@pushrax why not just change the behaviour of boolean expressions to the normal way, and put that change in the next major version? And we could allow people to opt in in the current version by using a declaration at the top of the file or something. |
I've used the It's not immediately clear how to use it from the README, so see the I agree that this is a big change to Liquid that will require tons of testing, but it would be nice to be able to write more complex |
Given what the compatibility requirements seem, maybe it'd be more realistic to require people to opt-in to the new syntax. I started sketching details, then realized I already did to some extent. The issue is recomposing conflicting requirements. @pushrax had volunteered to do the work, but added:
Options include:
@pushrax, @fw42, is option (1) doable on your end? The only disadvantage seems "ugliness" — I care about software's elegance, because it makes a practical difference, but it seriously seems less ugly than (2). Are there further obstacles? Thoughts? |
Yeah there are plenty of work arounds…but all I'm saying is if this patch is just sitting here waiting, the work arounds shouldn't be necessary.
|
Bump because I ran into this issue this afternoon and had to waste time understanding why "not" didn't exist and coming up with a workaround.. A "not" operator would be really nice |
lol |
Somehow, a template language that has been unable (or unwilling) to implement |
I also ran into this issue just now (not with Shopify, but GitHub Pages) and spent some time figuring out that It's really quite astonishing that there's no |
In case somebody stumbles over this in the future: Since {% if a %}
{% unless b %}
<p> a and not b </p>
{% endunless %}
{% endif %} Sadly there seems to be no good way to express an For more complex expressions I would recommend to use intermediate variables and evaluate the sub-expressions step by step. This is pretty tedious however and hard to maintain. |
This commit introduces a new Jekyll filter, which allows us to take the global `site.posts` collection and include or exclude certain tags. This is needed as the included `where_exp` filter doesn't allow you to negate collection contents and isn't being implemented. Shopify/liquid#138
This commit introduces a new Jekyll filter, which allows us to take the global `site.posts` collection and include or exclude certain tags. This is needed as the included `where_exp` filter doesn't allow you to negate collection contents and isn't being implemented. Shopify/liquid#138
This commit introduces a new Jekyll filter, which allows us to take the global `site.posts` collection and include or exclude certain tags. This is needed as the included `where_exp` filter doesn't allow you to negate collection contents and isn't being implemented. Shopify/liquid#138
Hi, in if ( A || ( B && C ) ) {
// Code here
} // How I can do with Liquid? {% if A or B and C %}
Hello
{% endif %} |
Any news? |
I don't know if this is really the same issue, but i'd love a not-where clause for filtering lists. IE: That way anything that isn't If this is a different issue please let me know; i didn't find this exact thing already reported and wasn't sure if i should create something new. |
Any news 9 years later? :( |
Soon to be 10.. still no solution for this? |
Maybe they want to keep it around as a running gag. 🤡 |
Yes, more logic in Liquid would be nice. On the time period to resolve this issue, there is an Atlassian Cloud Jira card in that will be 11 years old in 4 days :)
|
To use a NOT statement in Liquid this worked for me: {% if a=12 %} |
@erikplatte |
+1 for the feature! I need to express the following logic: {%- if !layout.no_gitalk and !page.no_gitalk and (jekyll.environment == 'production' or site.debugging_gitalk) -%} Currently I will need three nested if/unless to express this logic... |
Is this on the table 11 years later? Would love a simpler way to negate an assignment than this: assign negated = true
if myvar
assign negated = false
endif |
"and" is and.
"or" is or.
but what is "not" ?
thanks!
The text was updated successfully, but these errors were encountered: