Twig's tags learning (Chinese) II

Source: Internet
Author: User
The set tag is mainly used to assign values to variables. {% Setfoo & amp; #39; foo & amp; #39 ;%}{ % setfoo [1, 2] %}{ % setfoo {& amp; #39; foo & amp; #39;: & amp; #39; bar & amp; #39 ;}%}{ % setfoo & amp; #39; foo &

Set tag
It is mainly used to assign values to variables.

 

 

{% Set foo = 'foo' %}
 
{% Set foo = [1, 2] %}
 
{% Set foo = {'foo': 'bar'} %}
 
{% Set foo = 'foo '~ 'Bar' %}
 
{% Set foo, bar = 'foo', 'bar' %}
{% Set foo = 'foo' %}

{% Set foo = [1, 2] %}

{% Set foo = {'foo': 'bar'} %}

{% Set foo = 'foo '~ 'Bar' %}

{% Set foo, bar = 'foo', 'bar' %}
'Foo '~ The 'bar' is a string connection.

Another use of set is to assign the content in the block to the variable.


{% Set foo %}

...

{% Endset %}
{% Set foo %}

...

{% Endset %}


Extends labels
This label indicates that this template inherits from another template. Like php, twig does not support multi-inheritance. Therefore, you can only have one extends tag, which must be at the top of the template.

Let's first define a "base template" base.html, which is like a skeleton.


 
 
 
{% Block head %}
 
{% Block title % }{% endblock %}-My Webpage 
{% Endblock %}
 
 
{% Block content %} {% endblock %}

{% Block footer %}
©Copyright 2011 by youHttp://domain.invalid/"> you.
{% Endblock %}

 
 



{% Block head %}

{% Block title % }{% endblock %}-My Webpage
{% Endblock %}


{% Block content %} {% endblock %}

{% Block footer %}
©Copyright 2011 by youHttp://domain.invalid/"> you.
{% Endblock %}



The {% block %} tag defines four blocks (block head, block title, block content, block footer), allowing the subtemplate to fill in the content. The role of block is to tell the template engine that the content here can be overwritten by the quilt template.

A sub-template is similar to this


{% Extends "base.html" %}
 
{% Block title %} Index {% endblock %}
{% Block head %}
{Parent ()}}
 
{% Endblock %}
{% Block content %}
Index

 
Welcome on my awesome homepage.

 
{% Endblock %}
{% Extends "base.html" %}

{% Block title %} Index {% endblock %}
{% Block head %}
{Parent ()}}

{% Endblock %}
{% Block content %}
Index


Welcome on my awesome homepage.


{% Endblock %}
Extendsis very critical. because of the powerful template engine, this template inherits from another template (base.html ). When the template engine parses the template to this template, it first loads the parent template. The extends tag should be the first tag in the template.

If the sub-template does not define block footer, the parent template will be replaced by the default value.


Note: The block tag names cannot be repeated. If you want to print the same block multiple times. You can use the block function.


{% Block title % }{% endblock %} 
{Block ('title ')}}
{% Block body %} {% endblock %}
{% Block title % }{% endblock %}
{Block ('title ')}}
{% Block body %} {% endblock %}

Parent block
You may need the content of the parent block. You can use the parent function, which is useful for example when you want to add content to a block rather than overwrite it.


{% Block sidebar %}
Table Of Contents
...
{Parent ()}}
{% Endblock %}
{% Block sidebar %}
Table Of Contents
...
{Parent ()}}
{% Endblock %}


Name endblock
The template engine allows you to name the end mark, which improves readability a lot. But I personally think it is useless.

{% Block sidebar %}
{% Block inner_sidebar %}
...
{% Endblock inner_sidebar %}
{% Endblock sidebar %}
{% Block sidebar %}
{% Block inner_sidebar %}
...
{% Endblock inner_sidebar %}
{% Endblock sidebar %}

Nested block
Allows you to generate nested blocks to form more complex blocks.


{% For item in seq %}

  • {% Block loop_item % }{{ item }}{% endblock %}
  •  
    {% Endfor %}
    {% For item in seq %}
  • {% Block loop_item % }{{ item }}{% endblock %}

  • {% Endfor %}


    Abbreviated block
    The two statements below are equivalent.


    {% Block title %}
    {Page_title | title }}
    {% Endblock %}
     
    {% Block title page_title | title %}
    {% Block title %}
    {Page_title | title }}
    {% Endblock %}

    {% Block title page_title | title %}

     


    Dynamic inheritance
    You can use a variable to inherit different templates.


    {% Extends some_var %}
    {% Extends some_var %}
    If the variable is a twig template object, you can also.


    $ Layout = $ twig-> loadTemplate ('Some _ layout_template.twig ');
     
    $ Twig-> display ('Template. twig ', array ('layout' => $ layout ));
    $ Layout = $ twig-> loadTemplate ('Some _ layout_template.twig ');

    $ Twig-> display ('Template. twig ', array ('layout' => $ layout ));
    In version 1.2, you can pass an array. twig will select the first existing template to inherit from.


    {% Extends parameters 'layout.html ', 'base_layout.html'] %}
    {% Extends parameters 'layout.html ', 'base_layout.html'] %}


    Conditional inheritance
    Let's take a look at this,


    {% Extends standalone? "Minimum.html": "base.html" %}
    {% Extends standalone? "Minimum.html": "base.html" %}

     

    Block tag
    See extends labels

     

     

    Include tag
    Load a template and return the rendered content. The loaded template can use the variable {% include 'header.html '%} of the current template}
    Body
    {% Include 'footer.html '%}
    {% Include 'header.html '%}
    Body
    {% Include 'footer.html '%}

    You can add variables to the template.


    {# The foo template will have access to the variables from the current context and the foo one #}
    {% Include 'foo' with {'foo': 'bar'} %}
     
    {% Set vars = {'foo': 'bar'} %}
    {% Include 'foo' with vars %}
    {# The foo template will have access to the variables from the current context and the foo one #}
    {% Include 'foo' with {'foo': 'bar'} %}

    {% Set vars = {'foo': 'bar'} %}
    {% Include 'foo' with vars %}
    You can also use the only keyword to prohibit loading templates from using the variables of the current template. you can only use the variable with When include {# only the foo variable will be accessible #}
    {% Include 'foo' with {'foo': 'bar'} only %}
     
    {# No variable will be accessible #}
    {% Include 'foo' only %}
    {# Only the foo variable will be accessible #}
    {% Include 'foo' with {'foo': 'bar'} only %}

    {# No variable will be accessible #}
    {% Include 'foo' only %}

    The loaded template name can also be a twig expression.


    {% Include some_var %}
    {% Include ajax? 'Ajax.html ': 'not_ajax.html' %}
    {% Include some_var %}
    {% Include ajax? 'Ajax.html ': 'not_ajax.html' %}
    You can also use the twig template object


    $ Template = $ twig-> loadTemplate ('Some _ template. twig ');
     
    $ Twig-> loadTemplate ('Template. twig ')-> display (array ('template' => $ template ));
    $ Template = $ twig-> loadTemplate ('Some _ template. twig ');

    $ Twig-> loadTemplate ('Template. twig ')-> display (array ('template' => $ template ));

    In version 1.2, the new content can be added with the ignore missing keyword in the template, so that when the template does not exist, no error will be thrown.

     

     

    {% Include "sidebar.html" ignore missing %}
    {% Include "sidebar.html" ignore missing with {'foo': 'Bar} %}
    {% Include "sidebar.html" ignore missing only %}
    {% Include "sidebar.html" ignore missing %}
    {% Include "sidebar.html" ignore missing with {'foo': 'Bar} %}
    {% Include "sidebar.html" ignore missing only %} added content in version 1.2. you can pass an array to include, which will automatically load the first existing template {% include existing 'page_detailed.html ', 'page.html '] %}
    {% Include parameter 'page_detailed.html ', 'page.html'] %}

    Import tag


    Twig allows some common code to be put into macros (macro). These macros are imported by different templates.

    There are two ways to import a template. you can import the entire template to a variable or import only a few macros

    If we have a personal helper, we can help you prepare a table ticket (forms.html)


    {% Macro input (name, value, type, size) %}
     
    {% Endmacro %}
     
    {% Macro textarea (name, value, rows) %}
    {Value | e }} 
    {% Endmacro %}
    {% Macro input (name, value, type, size) %}

    {% Endmacro %}

    {% Macro textarea (name, value, rows) %}
    {Value | e }}
    {% Endmacro %}
    The simplest and most flexible way is to import the entire template. (Import the template to the forms variable)


    {% Import 'forms.html 'as forms %}
     


    Username
     
    {Forms. input ('Username ')}}
     
    Password
     
    {Forms. input ('password', null, 'password ')}}
     
     

    {Forms. textarea ('Comment ')}}

     
    {% Import 'forms.html 'as forms %}


    Username

    {Forms. input ('Username ')}}

    Password

    {Forms. input ('password', null, 'password ')}}


    {Forms. textarea ('Comment ')}}


    Alternatively, you can import the template name to the current namespace. (Import the input, textarea, and rename the input as input_field)

     

    {% From 'forms.html 'import input as input_field, textarea %}
     


    Username
     
    {Input_field ('Username ')}}
     
    Password
     
    {Input_field ('password', '', 'password ')}}
     
     

    {Textarea ('Comment ')}}

     
    {% From 'forms.html 'import input as input_field, textarea %}


    Username

    {Input_field ('Username ')}}

    Password

    {Input_field ('password', '', 'password ')}}


    {Textarea ('Comment ')}}

    If it is the macros defined in the current template, you do not need to import it. you can directly use the special variable _ self.


    {# Index.html template #}
     
    {% Macro textarea (name, value, rows) %}
    {Value | e }} 
    {% Endmacro %}
     

    {{_ Self. textarea ('Comment ')}}

     
    {# Index.html template #}

    {% Macro textarea (name, value, rows) %}
    {Value | e }}
    {% Endmacro %}

    {{_ Self. textarea ('Comment ')}}


    Then you can still import _ self into a variable, although it looks very good... No ..


    {# Index.html template #}
     
    {% Macro textarea (name, value, rows) %}
    {Value | e }} 
    {% Endmacro %}
     
    {% Import _ self as forms %}
     

    {Forms. textarea ('Comment ')}}

     
    {# Index.html template #}

    {% Macro textarea (name, value, rows) %}
    {Value | e }}
    {% Endmacro %}

    {% Import _ self as forms %}

    {Forms. textarea ('Comment ')}}


    From Tag
    See import label

    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.