我需要基于来自控制器的值在twig中显示一个图像。例如,如果来自控制器的值是(Home rent或Rent或Rent或Rent),并且列表继续下去,那么就应该出现在一个住宅的图像中。
目前,我正在做的事情如下
{% if goal == "house" or goals == "House" or goal == "home" or goals == "Home" %}
<img src="{{ asset('bundles/bundlename/images/house.jpg') }}">
{% endif %}名单很长,很快就会失控。因此,我想简单地用树枝创建一个数组,并检查来自控制器的值是否存在于我在树枝上显示图像的数组上。
发布于 2015-10-27 07:06:35
可以使用{key: value}或[value1, value2]语法定义数组。阅读更多关于数组和树枝本身这里的信息。
你可以这样做:
{% set images = {
"house.jpg": ["house", "House", "home", "Home"]
... some more rules ...
} %}
{% for image, keys in images %}
{% if goal in keys %}
<img src="{{ asset('bundles/bundlename/images/' ~ image) }}">
{% endif %}
{% endfor %}此外,您还可以将代码简化为{% if goal|lower in keys %},如果始终必须检查这两种情况,则只能在小写中定义键。
发布于 2021-04-02 10:08:35
在树枝上定义数组
{% set section = { foo:{ heading:"bar" } } %}
{{ section.foo.heading }}
bar //output
{% set section = { foo:"bar" } } %}
{{ section.foo }}
bar //output
{% set section = { foo:"bar", one:"two" } } %}
{{ section.one }}
two //outputhttps://stackoverflow.com/questions/33361049
复制相似问题