已弃用
当在模板中使用 deprecated
标签时,Twig 会生成弃用通知(通过调用 trigger_error()
PHP 函数)
1 2 3
{# base.html.twig #}
{% deprecated 'The "base.html.twig" template is deprecated, use "layout.html.twig" instead.' %}
{% extends 'layout.html.twig' %}
您还可以通过以下方式弃用宏
1 2 3 4 5
{% macro welcome(name) %}
{% deprecated 'The "welcome" macro is deprecated, use "hello" instead.' %}
...
{% endmacro %}
请注意,默认情况下,弃用通知是静默的,既不显示也不记录。请参阅 Recipes 了解如何处理它们。
3.11
package
和 version
选项在 Twig 3.11 中添加。
您可以选择添加引入弃用的 package 和 version
1 2
{% deprecated 'The "base.html.twig" template is deprecated, use "layout.html.twig" instead.' package='twig/twig' %}
{% deprecated 'The "base.html.twig" template is deprecated, use "layout.html.twig" instead.' package='twig/twig' version='3.11' %}
注意
不要使用 deprecated
标签来弃用 block
,因为弃用可能无法始终正确触发。