Skip to content

How to express logic "and","or", "not"? #138

Open

Description

@mobailabs

"and" is and.

"or" is or.

but what is "not" ?

thanks!

Activity

  1. nickpearson commented on Sep 13, 2012

    @nickpearson
    Contributor

    To work around this, I've always used nested if/unless statements, which isn't pretty but it works.

    So, if you have this in a language that has a not operator:

    if a && b && !c
      # ...
    end

    then you can do this in Liquid:

    {% if a and b %}
      {% unless c %}
        ...
      {% endunless %}
    {% endif %}
    

    It can get tricky if you need complex else logic, but most times this works for me.

  2. phoet commented on Jun 9, 2013

    @phoet
    Contributor

    @dapengme what is your specific usecase? you can also use var != 'value'

  3. fw42 commented on Jun 16, 2013

    @fw42
    Contributor

    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.

  4. retorquere commented on Aug 17, 2014

    @retorquere

    Without "not", it is quite awkward to express something like "if not p.url contains '/'"

  5. pathawks commented on Aug 17, 2014

    @pathawks
    Contributor

    if not p.url contains '/'

    unless p.url contains '/'?

  6. retorquere commented on Aug 17, 2014

    @retorquere

    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.

  7. cam commented on Sep 9, 2014

    @cam

    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.

  8. courtyenn commented on Feb 16, 2015

    @courtyenn

    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.

  9. reopened this on Feb 19, 2015
  10. fw42 commented on Feb 19, 2015

    @fw42
    Contributor

    Reopened because of #526

  11. Blaisorblade commented on Feb 19, 2015

    @Blaisorblade

    From #526:

    same way having support for parentheses would be nice to have

    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

    ExpressionsAndOperators = /(?:\b(?:\s?and\s?|\s?or\s?)\b|(?:\s*(?!\b(?:\s?and\s?|\s?or\s?)\b)(?:#{QuotedFragment}|\S+)\s*)+)/o
    . I see why this is not on your roadmap — you'd need to replace that part of the parser.

  12. fw42 commented on Feb 19, 2015

    @fw42
    Contributor

    @trishume and @pushrax might have thoughts

  13. pushrax commented on Feb 20, 2015

    @pushrax
    Contributor

    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?)

  14. trishume commented on Feb 22, 2015

    @trishume
    Contributor

    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 cond, if* or if+. Though the fact that fancy expression features work in that tag but not if would be weird.

    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: {% if [this && (that || other)] %}

  15. 50 remaining items

  16. mahnunchik commented on Dec 7, 2021

    @mahnunchik

    Any news?

  17. pigsflew commented on Dec 21, 2021

    @pigsflew

    I don't know if this is really the same issue, but i'd love a not-where clause for filtering lists. IE: {% assign promos = site.pages | where: 'homepage', true | except: 'show', true %}.

    That way anything that isn't true will trigger the thing, even if it's not actually a 'falsey' value.

    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.

  18. rianfloo commented on Jan 12, 2022

    @rianfloo

    Any news 9 years later? :(

  19. moseleyi commented on Feb 23, 2022

    @moseleyi

    Soon to be 10.. still no solution for this?

  20. fer-rum commented on Apr 20, 2022

    @fer-rum

    Maybe they want to keep it around as a running gag. 🤡

  21. TenguTech commented on Jul 4, 2022

    @TenguTech

    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 :)

  22. erikplatte commented on Jul 28, 2022

    @erikplatte

    To use a NOT statement in Liquid this worked for me:
    Simply put the NOT true in an else statement

    {% if a=12 %}
    [put if true result here]
    {% else %}
    [put if NOT true result here]
    {% endif %}

  23. TWiStErRob commented on Jul 28, 2022

    @TWiStErRob

    @erikplatte {% unless a=12 %} exists for that reason... the problem in this issues is more complex boolean expressions, e.g. a and not b.

  24. UlyssesZh commented on Dec 5, 2022

    @UlyssesZh

    +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...

  25. tim-harding commented on Jul 23, 2024

    @tim-harding

    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
  26. added a commit that references this issue on May 2, 2025
  27. josephcagle commented on Dec 16, 2025

    @josephcagle

    yeah I kind of need an expression like not (...) right now...

  28. isaacbowen commented on Dec 17, 2025

    @isaacbowen

    miiight be a thing in liquid 6.0.0? see #2017, which appears to be sketching out that future

  29. rickydazla commented on Dec 17, 2025

    @rickydazla

    @isaacbowen unless I'm missing something i don't see anything in that PR which would infer support for not being planned?

  30. evulse commented on Dec 17, 2025

    @evulse
    Contributor

    The dropping of lax and other things in v6 likely means that Shopify is about to auto upgrade/convert all existing non compliant templates they have to be compliant with v6 and the strict mode.

    Overall a very positive sign that they will finally be able to make significant progress to taking quirks and limitations like this out of liquid.

    I'd say it's probably unlikely they would add something new like this in the major v6 change as it's more about getting a solid foundation.

    But definitely see how this unlocks things like this finally making it into versions beyond that even if it's a v6.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions