有没有办法从另一个扩展TWIG编译模板?

时间:2015-12-15 22:02:06

标签: php templates inheritance compilation twig

经过TWIG编译模板的多次测试和检查后,我得出的结论是它可能没有实现,而是在TWIG受到启发的模板引擎中(在python中使用Jinja2进行测试)

预期的行为如下:

主要模板:

{# 'list.twig' #}

{% import _self as macro %}

{% for e in results %}      
    {{ macro.func(e) }}
{% endfor %}

扩展模板:

{# 'product.list.twig' #}
{% extends 'list.twig' %}

{% macro func(e) %}
    This is the product: {{ e.id }}
{% endmacro %}

另一个扩展模板:

{# 'user.list.twig' #}
{% extends 'list.twig' %}

{% macro func(e) %}
    This is the user: {{ e.id }}
{% endmacro %}

这些模板已经过编译,但PHP编译模板中有一个警告,用于调用func编译模板中的函数list.twig。 虽然此行{% import _self as macro %}已编译为$context["macro"] = $this;,但在此模板中使用$this是无用的,因为TWIG继承只是在编译时重新创建完整模板而没有PHP继承。

实际上,所有3个编译模板都是从Twig_Template类继承PHP,而完全继承功能将继承主列表编译模板中的扩展编译列表模板。

我是否遗漏了extends/embed/include/use TWIG功能中的某些内容来重新创建此行为,或者它在实际的TWIG版本中是不可能的?

感谢您的帮助

0 个答案:

没有答案
相关问题