包含,扩展,使用,宏,嵌入在Twig之间的区别 [英] Difference between Include, Extends, Use, Macro, Embed in Twig
问题描述
Twig中的use
和include
有什么区别?
What is the difference between use
and include
in Twig?
包括
include
语句包括一个模板,并将该模板的渲染内容返回到当前模板中:
include
The
include
statement includes a template and returns the rendered content of that template into the current one:
{% include 'header.html' %}
Body here...
{% include 'footer.html' %}
使用
use
语句告诉Twig将blocks.html
中定义的块导入到当前模板中(就像宏,但用于块):
use
The use
statement tells Twig to import the blocks defined in blocks.html
into the current template (it's like macros, but for blocks):
blocks.html
blocks.html
{% block sidebar %}{% endblock %}
main.html
main.html
{% extends "base.html" %}
{% use "blocks.html" %}
{% block title %}{% endblock %}
{% block content %}{% endblock %}
可能的答案:
我认为这应该说明差异:
Possible answer:
I think this should explain the difference:
include
是从外部文件获取所有代码并将其导入 放入通话正确位置的实际文件中.
include
is to get all the code from an external file and import it into your actual file at the right location of the call.
use
完全不同,因为它解析链接的文件以找到一个
代码的特定部分,然后用
与您当前文件中的同名文件,以及在此外部文件中找到的同一个文件名
文件.
use
is completely different as it parses the linked file to find a
particular section of code and then overwrites the blocks with the
same name, in your current file, with the one found in this external
file.
include
就像"找到此文件并在此处与我的页面呈现".
use
为"解析此其他文件以查找要使用的块定义
我自己的定义在这里".
use
is "parse this other file to find block definitions to use instead
of my owns defined here".
如果use
命令未找到与任务匹配的内容,则不会显示任何内容
完全来自此文件.
If use
command finds nothing matching the task, nothing is displayed
at all from this file.
问题
解释正确吗?对于这种差异还有其他解释吗?
Question
is the explanation correct? are there any other explanations to this difference?
推荐答案
几个月后,我将发布答案,以获取对该问题的进一步参考.我还为extends
&添加了一些说明. import
& macro
& embed
以获得更多许可:
After months, I am posting an answer for any further reference to this question. I also added some description for extends
& import
& macro
& embed
for more clearance:
Twig中有多种类型的继承和代码重用:
There are various types of inheritance and code reuse in Twig:
主要目标:代码重用
用例:使用header.html.twig
& base.html.twig
内的footer.html.twig
.
Use Case: Using header.html.twig
& footer.html.twig
inside base.html.twig
.
header.html.twig
<nav>
<div>Homepage</div>
<div>About</div>
</nav>
footer.html.twig
<footer>
<div>Copyright</div>
</footer>
base.html.twig
{% include 'header.html.twig' %}
<main>{% block main %}{% endblock %}</main>
{% include 'footer.html.twig' %}
扩展
主要目标:垂直重用
Extends
Main Goal: Vertical Reuse
用例:在homepage.html.twig
内部扩展base.html.twig
& about.html.twig
.
Use Case: Extending base.html.twig
inside homepage.html.twig
& about.html.twig
.
base.html.twig
{% include 'header.html.twig' %}
<main>{% block main %}{% endblock %}</main>
{% include 'footer.html.twig' %}
homepage.html.twig
{% extends 'base.html.twig' %}
{% block main %}
<p>Homepage</p>
{% endblock %}
about.html.twig
{% extends 'base.html.twig' %}
{% block main %}
<p>About page</p>
{% endblock %}
使用
主要目标:横向重用
Use
Main Goal: Horizontal Reuse
用例: single.product.html.twig
和single.product.html.twig
中的sidebar.html.twig
single.service.html.twig
.
sidebar.html.twig
{% block sidebar %}<aside>This is sidebar</aside>{% endblock %}
single.product.html.twig
{% extends 'product.layout.html.twig' %}
{% use 'sidebar.html.twig' %}
{% block main %}<main>Product page</main>{% endblock %}
single.service.html.twig
{% extends 'service.layout.html.twig' %}
{% use 'sidebar.html.twig' %}
{% block main %}<main>Service page</main>{% endblock %}
注释:
- 这就像宏,但用于块.
- use标签仅在不扩展另一个模板,未定义宏且主体为空的情况下才导入模板.
宏
主要目标:带有变量的可重用标记
Macro
Main Goal: Reusable Markup with Variables
用例:一个获取一些变量并输出一些标记的函数.
Use Case: A function which gets some variables and outputs some markup.
form.html.twig
{% macro input(name, value, type) %}
<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" }}" />
{% endmacro %}
profile.service.html.twig
{% import "form.html.twig" as form %}
<form action="/login" method="post">
<div>{{ form.input('username') }}</div>
<div>{{ form.input('password') }}</div>
<div>{{ form.input('submit', 'Submit', 'submit') }}</div>
</form>
嵌入
主要目标:阻止覆盖
Embed
Main Goal: Block Overriding
用例:将pagination.html.twig
嵌入product.table.html.twig
和& service.table.html.twig
.
Use Case: Embedding pagination.html.twig
in product.table.html.twig
& service.table.html.twig
.
pagination.html.twig
<div id="pagination">
<div>{% block first %}{% endblock %}</div>
{% for i in (min + 1)..(max - 1) %}
<div>{{ i }}</div>
{% endfor %}
<div>{% block last %}{% endblock %}</div>
</div>
product.table.html.twig
{% set min, max = 1, products.itemPerPage %}
{% embed 'pagination.html.twig' %}
{% block first %}First Product Page{% endblock %}
{% block last %}Last Product Page{% endblock %}
{% endembed %}
service.table.html.twig
{% set min, max = 1, services.itemPerPage %}
{% embed 'pagination.html.twig' %}
{% block first %}First Service Page{% endblock %}
{% block last %}Last Service Page{% endblock %}
{% endembed %}
请注意,嵌入式文件(pagination.html.twig
)可以访问当前上下文(min
,max
变量).
Please note that embedded file (pagination.html.twig
) has access to the current context (min
, max
variables).
您还可以将其他变量传递给嵌入式文件:
Also you may pass extra variables to the embedded file:
pagination.html.twig
<p>{{ count }} items</p>
<div>
<div>{% block first %}{% endblock %}</div>
{% for i in (min + 1)..(max - 1) %}
<div>{{ i }}</div>
{% endfor %}
<div>{% block last %}{% endblock %}</div>
</div>
product.table.html.twig
{% set min, max = 1, products|length %}
{% embed 'pagination.html.twig' with {'count': products|length } %}
{% block first %}First Product Page{% endblock %}
{% block last %}Last Product Page{% endblock %}
{% endembed %}
注意:
它既具有Use
的功能,又具有Include
一起.
It has functionality of both Use
& Include
together.
这篇关于包含,扩展,使用,宏,嵌入在Twig之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!