Opencart 3 TWIG:如何使用AND运算符

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

Opencart 3 TWIG: how to use AND operator

问题

我尝试在Opencart 3中的TWIG中使用"AND"运算符,但它不起作用。请告诉我我做错了什么。

我有一些产品属性。我想要创建一个条件,如果其中两个具有特定ID的属性存在,那么条件就满足了。

{% for attribute_group in attribute_groups %}
    {% for attribute in attribute_group.attribute %}
        {% if attribute.attribute_id == 2 and attribute.attribute_id == 3 %}
            第一个条件
        {% elseif attribute.attribute_id == 2 %}
            第二个条件
        {% elseif attribute.attribute_id == 3 %}
            第三个条件
        {% else %}
        {% endif %}
    {% endfor %}
{% endfor %}

以下是文本示例:

如果有一个属性的ID等于2和一个属性的ID等于3,则写入"Floor/Number of floors"。
如果只有一个属性的ID等于2,则写入"Floor"。
如果只有一个属性的ID等于3,则写入"Numbers of floors"。
英文:

I'm trying to use the "AND" operator in TWIG in Opencart 3 and it doesn't work. Please tell me what I'm doing wrong.

I have some product attributes. And I want to make a condition that if two of the attribute with a specific ID are there, then the condition is met.

{% for attribute_group in attribute_groups %}
    {% for attribute in attribute_group.attribute %}
        {% if attribute.attribute_id == 2 and attribute.attribute_id == 3 %}
            First condition
        {% elseif attribute.attribute_id == 2 %}
            Second condition
        {% elseif attribute.attribute_id == 3 %}
            Third condition
        {% else %}
        {% endif %}
    {% endfor %}
{% endfor %

Here is text example:

if there is an attribute with ID equal 2 and an attribute with ID equal 3 then write "Floor/Number of floors". 
if there is an attribute with ID equal 2 only then write "Floor"
if there is an attribute with ID equal 3 only then write "Numbers of floors".

答案1

得分: 1

以下是您要翻译的内容:

无法同时是`X`和`Y`。此外,我建议您在控制器中进行测试,而不是在视图中。

无论如何,如果您想在视图中执行此操作,您需要跟踪找到的属性。您可以使用两个布尔值或只是添加一个计数器来完成。

{% set cnt = 0 %}
{% for attribute_group in attribute_groups %}
    {% for attribute in attribute_group.attribute %}
        {% if attribute.attribute_id == 2 or attribute.attribute_id == 3 %}
            {% set cnt = cnt + 1 %}
        {% endif %}
    {% endfor %}
{% endfor %}

{% if cnt == 2 %}
    {# 执行某些操作 #}
{% endif %}

您可以使用in测试来简化if语句:

{% if attribute.attribute_id in [2, 3,] %}

更新我的答案,因为OP更改了问题的要求:

{% set words = [] %}
{% for attribute_group in attribute_groups %}
    {% for attribute in attribute_group.attribute %}
        {% if attribute.attribute_id == 2  %}
            {% set words = words|merge(['楼层',]) %}
        {% elseif attribute.attribute_id == 3  %}
            {% set words = words|merge(['楼层数',]) %}
        {% endif %}
    {% endfor %}
{% endfor %}

{% if words|default  %}
    {{ words|join('/') }}
{% endif %}

演示


<details>
<summary>英文:</summary>

Something can&#39;t be both `X` and `Y` at the same time. Furthermore this is something I&#39;d advice you to test in the controller and not in the view.

Anyway if you wanted to do this in the view you will need to track of the found attributes. You could do this with two booleans or just add a counter.

{% set cnt = 0 %}
{% for attribute_group in attribute_groups %}
{% for attribute in attribute_group.attribute %}
{% if attribute.attribute_id == 2 or attribute.attribute_id == 3 %}
{% set cnt = cnt + 1 %}
{% endif %}
{% endfor %}
{% endfor %}

{% if cnt == 2 %}
{# do something #}
{% endif %}


You can simplify the if by using the test `in`

{% if attribute.attribute_id in [2, 3,] %}

-----

Update of my answer because OP changed the requirements of the question

{% set words = [] %}
{% for attribute_group in attribute_groups %}
{% for attribute in attribute_group.attribute %}
{% if attribute.attribute_id == 2 %}
{% set words = words|merge(['Floor',]) %}
{% elseif attribute.attribute_id == 3 %}
{% set words = words|merge(['Numbers of floors',]) %}
{% endif %}
{% endfor %}
{% endfor %}

{% if words|default %}
{{ words|join('/') }}
{% endif %}


[demo](https://twigfiddle.com/zcydg9)

</details>



huangapple
  • 本文由 发表于 2023年2月14日 00:24:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438589.html
匿名

发表评论

匿名网友

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

确定