我已经在Twig中添加了一个宏,并且我正在尝试让这个宏调用它自己。现在看来,使用_self似乎不太受欢迎,而且不起作用,返回错误:
using the dot notation on an instance of Twig_Template is deprecated since version 1.28 and won't be supported anymore in 2.0.如果我确实将_self作为x导入,那么当我最初调用宏时,它就会工作:
{% import _self as twigdebug %}
{{ twigdebug.recursiveTree() }}但是我不能使用_self或twigdebug.recursiveTree递归调用宏。
有没有办法做到这一点?
发布于 2017-01-02 19:40:42
示例:
{% macro recursiveCategory(category) %}
    {% import _self as self %}
    <li>
        <h4><a href="{{ path(category.route, category.routeParams) }}">{{ category }}</a></h4>  
        {% if category.children|length %}
            <ul>
                {% for child in category.children %}
                    {{ self.recursiveCategory(child) }}
                {% endfor %}
            </ul>
        {% endif %}
    </li>
{% endmacro %}
{% from _self import recursiveCategory %}
<div id="categories">
    <ul>
        {% for category in categories %}
            {{ recursiveCategory(category) }}
        {% endfor %}
    </ul>
</div>发布于 2017-01-30 22:26:18
它是用Twig的macro文档编写的:
细枝宏没有访问当前模板变量的权限
您必须在模板和宏中import self:
{% macro recursiveTree() %}
    {# ... #}
    {# Import and call from macro scope #}
    {% import _self as twigdebug %}
    {{ twigdebug.recursiveTree() }}
{% endmacro %}
{# Import and call from template scope #}
{% import _self as twigdebug %}
{{ twigdebug.recursiveTree() }}或者,可以将导入的_self对象直接传递给宏。
{% macro recursiveTree(twigdebug) %}
    {# ... #}
    {# Call from macro parameter #}
    {# and add the parameter to the recursive call #}
    {{ twigdebug.recursiveTree(twigdebug) }}
{% endmacro %}
{# Import and call from template scope #}
{% import _self as twigdebug %}
{{ twigdebug.recursiveTree(twigdebug) }}https://stackoverflow.com/questions/40707461
复制相似问题