Twig's tags Learning (Chinese) II

Source: Internet
Author: User
Tags vars

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 %}
<Div id = "pagination">
...
</Div>
{% Endset %}
{% Set foo %}
<Div id = "pagination">
...
</Div>
{% 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.


<! DOCTYPE html>
<Html>
<Head>
{% Block head %}
<Link rel = "stylesheet" href = "style.css"/>
<Title >{% block title % }{% endblock %}-My Webpage </title>
{% Endblock %}
</Head>
<Body>
<Div id = "content" >{% block content %}{% endblock %}</div>
<Div id = "footer">
{% Block footer %}
©Copyright 2011 by <a href = "http://domain.invalid/"> you </a>.
{% Endblock %}
</Div>
</Body>
</Html>
<! DOCTYPE html>
<Html>
<Head>
{% Block head %}
<Link rel = "stylesheet" href = "style.css"/>
<Title >{% block title % }{% endblock %}-My Webpage </title>
{% Endblock %}
</Head>
<Body>
<Div id = "content" >{% block content %}{% endblock %}</div>
<Div id = "footer">
{% Block footer %}
©Copyright 2011 by <a href = "http://domain.invalid/"> you </a>.
{% Endblock %}
</Div>
</Body>
</Html>
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 ()}}
<Style type = "text/css">
. Important {color: #336699 ;}
</Style>
{% Endblock %}
{% Block content %}
<H1> Index <P class = "important">
Welcome on my awesome homepage.
</P>
{% Endblock %}
{% Extends "base.html" %}

{% Block title %} Index {% endblock %}
{% Block head %}
{Parent ()}}
<Style type = "text/css">
. Important {color: #336699 ;}
</Style>
{% Endblock %}
{% Block content %}
<H1> Index <P class = "important">
Welcome on my awesome homepage.
</P>
{% 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.


<Title >{% block title % }{% endblock %} </title>
<H1 >{{ block ('title') }}{% Block body %} {% endblock %}
<Title >{% block title % }{% endblock %} </title>
<H1 >{{ 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 %}
<H3> Table Of Contents ...
{Parent ()}}
{% Endblock %}
{% Block sidebar %}
<H3> 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 %}
<Li >{% block loop_item % }{{ item }}{% endblock %} </li>
{% Endfor %}
{% For item in seq %}
<Li >{% block loop_item % }{{ item }}{% endblock %} </li>
{% 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) %}
<Input type = "{type | default ('text ')}} "name =" {name} "value =" {value | e} "size =" {size | default (20)} "/>
{% Endmacro %}
 
{% Macro textarea (name, value, rows) %}
<Textarea name = "{name}" rows = "{rows | default (10)}" cols = "{cols | default (40 )}} ">{{ value | e }}</textarea>
{% Endmacro %}
{% Macro input (name, value, type, size) %}
<Input type = "{type | default ('text ')}} "name =" {name} "value =" {value | e} "size =" {size | default (20)} "/>
{% Endmacro %}

{% Macro textarea (name, value, rows) %}
<Textarea name = "{name}" rows = "{rows | default (10)}" cols = "{cols | default (40 )}} ">{{ value | e }}</textarea>
{% 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 %}
 
<Dl>
<Dt> Username </dt>
<Dd >{{ forms. input ('username') }}</dd>
<Dt> Password </dt>
<Dd >{{ forms. input ('Password', null, 'Password') }}</dd>
</Dl>
<P >{{ forms. textarea ('comment') }}</p>
{% Import 'forms.html 'as forms %}

<Dl>
<Dt> Username </dt>
<Dd >{{ forms. input ('username') }}</dd>
<Dt> Password </dt>
<Dd >{{ forms. input ('Password', null, 'Password') }}</dd>
</Dl>
<P >{{ forms. textarea ('comment') }}</p>
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 %}
 
<Dl>
<Dt> Username </dt>
<Dd >{{ input_field ('username') }}</dd>
<Dt> Password </dt>
<Dd >{{ input_field ('Password', '', 'Password') }}</dd>
</Dl>
<P >{{ textarea ('comment') }}</p>
{% From 'forms.html 'import input as input_field, textarea %}

<Dl>
<Dt> Username </dt>
<Dd >{{ input_field ('username') }}</dd>
<Dt> Password </dt>
<Dd >{{ input_field ('Password', '', 'Password') }}</dd>
</Dl>
<P >{{ textarea ('comment') }}</p> if it is the macros defined in the current template, you do not need to import it. Use the special Variable _ self.


{# Index.html template #}
 
{% Macro textarea (name, value, rows) %}
<Textarea name = "{name}" rows = "{rows | default (10)}" cols = "{cols | default (40 )}} ">{{ value | e }}</textarea>
{% Endmacro %}
 
<P >{{_ self. textarea ('comment') }}</p>
{# Index.html template #}

{% Macro textarea (name, value, rows) %}
<Textarea name = "{name}" rows = "{rows | default (10)}" cols = "{cols | default (40 )}} ">{{ value | e }}</textarea>
{% Endmacro %}

<P >{{_ self. textarea ('comment') }}</p>
Then you can still import _ self into a variable, although it looks very good... No ..


{# Index.html template #}
 
{% Macro textarea (name, value, rows) %}
<Textarea name = "{name}" rows = "{rows | default (10)}" cols = "{cols | default (40 )}} ">{{ value | e }}</textarea>
{% Endmacro %}
 
{% Import _ self as forms %}
 
<P >{{ forms. textarea ('comment') }}</p>
{# Index.html template #}

{% Macro textarea (name, value, rows) %}
<Textarea name = "{name}" rows = "{rows | default (10)}" cols = "{cols | default (40 )}} ">{{ value | e }}</textarea>
{% Endmacro %}

{% Import _ self as forms %}

<P >{{ forms. textarea ('comment') }}</p>


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.