End of Twig's tags Learning (Chinese) 3

Source: Internet
Author: User

Use tag
The use tag is newly added to version 1.1.
This use Tag mainly solves the problem that the template can only inherit from one parent template, and you want to reuse Other templates. However, the use tag only imports block blocks,
(Note that import only imports macro macros, and include imports everything. The three labels must be clearly distinguished)
For example, {% extends "base.html" %}
 
{% Use "blocks.html" %}
 
{% Block title % }{% endblock %}
{% Block content %} {% endblock %}
{% Extends "base.html" %}

{% Use "blocks.html" %}

{% Block title % }{% endblock %}
{% Block content %} {% endblock failed while the content of blocks.html is
# Blocks.html
{% Block sidebar % }{% endblock %}
# Blocks.html
{% Block sidebar %} {% endblock detail failed. We imported the block sidebar from blocks..html.
The running result is almost equal
{% Extends "base.html" %}
 
{% Block sidebar % }{% endblock %}
{% Block title % }{% endblock %}
{% Block content %} {% endblock %}
{% Extends "base.html" %}

{% Block sidebar % }{% endblock %}
{% Block title % }{% endblock %}
{% Block content %} {% endblock %}
The template imported by the usetag (block.html in the previous example) cannot inherit other templates or define macro macros. However, it can use other templates.
In addition, the file name after the use tag cannot be an expression.


When the imported block and the main template block have the same name, the template engine will automatically ignore the imported block by the use label.
To avoid this situation. You can rename the block when using the use tag.
{% Extends "base.html" %}
 
{% Use "blocks.html" with sidebar as base_sidebar %}
 
{% Block sidebar % }{% endblock %}
{% Block title % }{% endblock %}
{% Block content %} {% endblock %}
{% Extends "base.html" %}

{% Use "blocks.html" with sidebar as base_sidebar %}

{% Block sidebar % }{% endblock %}
{% Block title % }{% endblock %}
{% Block content %} {% endblock %}
Version 1.3 supports the parent () function (this is particularly important)
The parent () function automatically handles the inheritance tree of the block. If you overwrite the block introduced by the use label in the main template, the parent () function is used () the function can call the covered block content.
{% Extends "base.html" %}
 
{% Use "blocks.html" %}
 
{% Block sidebar %}
{Parent ()}}
{% Endblock %}
 
{% Block title % }{% endblock %}
{% Block content %} {% endblock %}
{% Extends "base.html" %}

{% Use "blocks.html" %}

{% Block sidebar %}
{Parent ()}}
{% Endblock %}

{% Block title % }{% endblock %}
{% Block content %} {% endblock %}
Note that the content of parent () is actually the content of block sidebar in blocks.html. Because the inheritance tree is base.html-> blocks.html-> This template


If you rename the imported block in the use tag, you can use the block function to replace the effect of the parent function in the code above.
{% Extends "base.html" %}
 
{% Use "blocks.html" with sidebar as parent_sidebar %}
 
{% Block sidebar %}
{Block ('parent _ sidebar ')}}
{% Endblock %}
{% Extends "base.html" %}

{% Use "blocks.html" with sidebar as parent_sidebar %}

{% Block sidebar %}
{Block ('parent _ sidebar ')}}
{% Endblock %}
You can use any number of use tags. If multiple use tags have duplicate block names, the last use tag is valid.
Spacelsee label
The white space between html tags is deleted.
{% Spaceless %}
<Div>
<Strong> foo </strong>
</Div>
{% Endspaceless %}
 
{# Output will be <div> <strong> foo </strong> </div> #}
{% Spaceless %}
<Div>
<Strong> foo </strong>
</Div>
{% Endspaceless %}

{# Output will be <div> <strong> foo </strong> </div> #}
Autoescape label
I did not understand this. I only know that the literal meaning is automatic escape .. But .. I still don't know how to use it during my experiments.
His official example is:
{% Autoescape true %}
Everything will be automatically escaped in this block
{% Endautoescape %}
 
{% Autoescape false %}
Everything will be outputed as is in this block
{% Endautoescape %}
 
{% Autoescape true js %}
Everything will be automatically escaped in this block
Using the js escaping strategy
{% Endautoescape %}
{% Autoescape true %}
Everything will be automatically escaped in this block
{% Endautoescape %}

{% Autoescape false %}
Everything will be outputed as is in this block
{% Endautoescape %}

{% Autoescape true js %}
Everything will be automatically escaped in this block
Using the js escaping strategy
{% Endautoescape %}
I did this test. The output is the original content.
{% Autoescape true %}
<Body> <B> aaaa </B> </body>
{% Endautoescape %}
 
{% Autoescape false %}
<B> aaaa </B>
{% Endautoescape %}
 
{% Autoescape true js %}
<Script>
Function aaa () {alert ('x ');}
</Script>
{% Endautoescape %}
{% Autoescape true %}
<Body> <B> aaaa </B> </body>
{% Endautoescape %}

{% Autoescape false %}
<B> aaaa </B>
{% Endautoescape %}

{% Autoescape true js %}
<Script>
Function aaa () {alert ('x ');}
</Script>
{% Endautoescape %}
I would like to ask you to pass...
The official documentation also says that if {% autoescape true %} is used, the content in it will be converted to safe content unless you use the raw filter.
{% Autoescape true %}
{Safe_value | raw }}
{% Endautoescape %}
{% Autoescape true %}
{Safe_value | raw }}
{% Endautoescape %}
In addition, the return values of functions in twig are safe, such as macros parent.


Raw tag
Raw labels ensure that the data in the block is not parsed by the template engine. {% Raw %}
<Ul>
{% For item in seq %}
<Li >{{ item }}</li>
{% Endfor %}
</Ul>
{% Endraw %}
{% Raw %}
<Ul>
{% For item in seq %}
<Li >{{ item }}</li>
{% Endfor %}
</Ul>
{% Endraw %}
Flush tag
New Content in version 1.5
Tell the template that the flush function of php is actually called internally to refresh the output cache.
{% Flush %}
{% Flush %}

Do tag
New Content in version 1.5
The do tag serves as an output tag {}. It can calculate some expressions. The difference is that nothing is printed.
{% Do 1 + 2%}
{% Do 1 + 2%}


The learning of tags ends now. Give me a round of applause ..... Next, go to the filter learning ...... Cool

From the column jiaochangyun

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.