Twig:如何设置对象变量?

时间:2016-06-26 16:16:51

标签: twig

我尝试在Twig模板中创建来自存在对象变量的新变量( filter 是对象):

{% for filter in filters %}
    {% if filter.type != 'selectImage' %}
        {{ filter.render()|raw }}
    {% elseif filter.type == 'selectImage'%}
        {% set selectFilter = filter %}
    {% endif %}
{% endfor %}

但我收到错误:

Catchable fatal error: Object of class Filters\Filter could not be converted to string in vendor\twig\twig\lib\Twig\Environment.php(403) : eval()'d code on line 40

{% set selectFilter = filter %}

我如何将对象设置为新的可变对象?

1 个答案:

答案 0 :(得分:3)

根据official TWIG documentationfilter是TWIG语言中的标记名称。

您应该在代码中重命名变量,以避免生成的PHP代码出现问题:

{% set selectFilter = myFilter %}