我有一个多维数组,其中一些对象存在而另一些不存在。页面中使用了整个数据。然后我计划在 TWIG 中检查它。 示例数据:
array:2[
0 => Data1 {
-id: 17
-porodType: "1d"
-name: "Dally promotion"
}
1 => Data1 {
-id: 34
-porodType: "S"
-name: "Special"
}
]
如何检查响应中是否存在 porodType = "1d"的记录? 如何为此操作显示不同的消息:存在(OK)/不存在(错误)?
checkin TWIG时:
{% for d in Data1 %}
{% if d.porodType == '1d' %}
<button class="btn">OK</button>
{% else %}
<button class="btn"">Error</button>
{% endif %}
{% endfor %}
这段代码的结果是:<button class="btn">OK</button><button class="btn">Error</button>
但我期望<button class="btn">OK</button>
或 <button class="btn">ERROR</button>
最佳答案
如果你只想显示一个按钮,你需要用一个标志来跟踪错误,因为你不能在 Twig
中打破循环,
{% set error = false %}
{% for d in Data1 %}
{% if d.porodType != '1d' %}
{% set error = true %}
{% endif %}
{% endfor %}
{% if error %}
<button class="btn">Error</button>
{% else %}
<button class="btn">OK</button>
{% endif %}
关于php - 如何检查响应 TWIG 中是否存在记录 - 表格格式的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44272365/