有两种不同的方式可以包含一个文件:
{% include 'fic.html.twig' %}
{{ include('fic.html.twig') }}
这两种方法有什么区别?
来源:
有两种不同的方式可以包含一个文件:
{% include 'fic.html.twig' %}
{{ include('fic.html.twig') }}
这两种方法有什么区别?
来源:
标签比函数不够灵活,例如:
如果你想将文件的内容存储在变量中以便重复使用:
{% set content = include('test.twig') %}
而不是:
{% set content %}
{% include 'test.twig' %}
{% endset %}
如果你想要添加过滤器:
{{ include('alert.twig') | upper }}
它的标签等效形式:
{% set temp %}
{% include 'alert.twig' %}
{% endset %}
{{ temp | upper }}
{{ include }}
而不是{% include %}
虽然不会改变世界,但在你需要使用Twig进行一些棘手操作时可以减少一些复杂性。{{ include() }}
以符合最佳实践:{{ }} is used to print the result of an expression evaluation;
{% %} is used to execute statements.
来自Twig的更新日志:
* 1.12.0-RC1 (2012-12-29)
* added an include function (does the same as the include tag but in a more flexible way)
{% include '' %}
是一个标签,而 {{ include('') }}
是一个函数。也许如果你想要覆盖这个函数,使用函数会更容易一些,而不是使用标签。