英文:
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 %}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论