Check if variable is string or array in Twig

后端 未结 4 567
暗喜
暗喜 2020-12-24 10:29

Is it possible to check if given variable is string in Twig ?

Expected solution:

messages.en.yml:

hello:
  stranger         


        
4条回答
  •  無奈伤痛
    2020-12-24 11:08

    Can be done with the test iterable, added in twig1.7, as Wouter J stated in the comment :

    {# evaluates to true if the foo variable is iterable #}
    {% if users is iterable %}
        {% for user in users %}
            Hello {{ user }}!
        {% endfor %}
    {% else %}
        {# users is probably a string #}
        Hello {{ users }}!
    {% endif %}
    

    Reference : iterable

提交回复
热议问题