Jekyll Liquid If-Or-Condition is False, even though one of the two conditions should return true

huangapple go评论52阅读模式
英文:

Jekyll Liquid If-Or-Condition is False, even though one of the two conditions should return true

问题

To summarize, I have an array including several arrays that look like this:
[0, 1, 2, 3, {'key': 'example', 'id': 0}].

I have a loop that looks like this:

{% for arr in array %} // Array including the array I showed above
  {% for item in arr %} // Array of pattern I showed above

    {% if item.key == key and item.id == id %}
      If-1 True
    {% else %}
      If-2 False
    {% endif %}

    {% if (elem == key and item == id) or (item.key == key and item.id == id ) %}
      If-2 True
    {% else %}
      If-2 False
    {% endif %}

  {% endloop %}
{% endloop %}

As you can see, the second If-Statement includes the condition of the first If statement, as well as a second condition with an or between. As I am understanding from my past experience in programming, this should mean that if either of the two conditions are true, the whole If-Statement should be true.

However, this is not the case here. The first Statement will return the If-1 True String when the second Statement still returns the If-2 False String.

Why is this the case? Does or work differently in Liquid?

英文:

To summarize, I have an array including several arrays that look like this:
[0, 1, 2, 3, {'key': 'example', 'id': 0}].

I have a loop that looks like this:

{% for arr in array %} // Array including the array I showed above
  {% for item in arr %} // Array of pattern I showed above

    {% if item.key == key and item.id == id %}
      If-1 True
    {% else %}
      If-2 False
    {% endif %}

    {% if (elem == key and item == id) or (item.key == key and item.id == id ) %}
      If-2 True
    {% else %}
      If-2 False
    {% endif %}

  {% endloop %}
{% endloop %}

As you can see, the second If-Statement includes the condition of the first If statement, as well as a second condition with an or between. As I am understanding from my past experience in programming, this should mean that if either of the two conditions are true, the whole If-Statement should be true.

However this is not the case here. The first Statement will return the If-1 True String when the second Statement still returns the If-2 False String.

Why is this the case? Does or work differently in Liquid?

答案1

得分: 1

You're missing a closing ) bracket on the or line at the end. That might solve it:

{% if (elem == key and item == id) or (item.key == key and item.id == id) %}

I've had issues like this in the past (years ago), and worked around them by assigning each side of the or to a separate variable first. No idea why but this might help:

{% assign a = elem == key and item == id %}
{% assign b = item.key == key and item.id == id %}
{% if a or b %}
英文:

You're missing a closing ) bracket on the or line at the end. That might solve it:

{% if (elem == key and item == id) or (item.key == key and item.id == id) %}

I've had issues like this in the past (years ago), and worked around them by assigning each side of the or to a separate variable first. No idea why but this might help:

{% assign a = elem == key and item == id %}
{% assign b = item.key == key and item.id == id %}
{% if a or b %}

huangapple
  • 本文由 发表于 2023年3月21日 01:07:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793267.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定