2 回答
TA贡献1851条经验 获得超3个赞
您可以遍历代码数组:
{% for c in code %}
{# ... #}
{% endfor %}
文档: https : //twig.symfony.com/doc/2.x/tags/for.html
然后,如果项目确实匹配:
{# ... #}
{% if airport == c %}
{# ... #}
{% endif %}
{# ... #}
文档: https : //twig.symfony.com/doc/2.x/tags/if.html
在相同的循环索引处替换变量 airport:
{# ... #}
{% set airport = city[loop.index0] %}
{# ... #}
文档: https : //twig.symfony.com/doc/2.x/tags/for.html#the-loop-variable
所以,完整的:
{% for c in code %}
{% if airport == c %}
{% set airport = city[loop. index0] %}
{% endif %}
{% endfor %}
运行小提琴:https : //twigfiddle.com/xflfas/2
超出范围说明:您的数组最好命名为cities和codes。
这样,当你遍历它们时,你最终会得到有意义的命名
{% set codes = ['AMS', 'EIN', 'RTM'] %}
{% for code in codes %}
{{ code }}
{% endfor %}
{# and #}
{% set cities = ['Amsterdam', 'Eindhoven', 'Rotterdam'] %}
{% for city in cities %}
{{ city }}
{% endfor %}
TA贡献1856条经验 获得超11个赞
用于{% if value in array %}
从第一个数组和 Twig 的合并函数中搜索以替换第二个数组中的值。请参阅此https://twig.symfony.com/doc/2.x/filters/merge.html
- 2 回答
- 0 关注
- 164 浏览
添加回答
举报