Django framework (XI): template description, language templates, template inheritance, HTML escape

1. Introduction Template

1.1 template functions

Generate html, control content displayed on the page. Html template file is not just a file.

The template file contains two parts:

Static content: css, js, html.

Dynamic content: for dynamically to generate some page content. Generated by the template language.

1.2 Use template files

Usually returned to the client using templates to generate html content in the view function.

Load the template file loader.get_template, to obtain the contents of the template file, producing a template object.

Custom template context RequeseContext, pass data to the template file.

Template generated html rendering the render page content, the replacement data corresponding variable transmission, after generating an alternative standard html content.

1.3 template file load order

First template directory to find the configuration template file.

INSTALLED_APPS templates to each of the following applications to find the template file, if the application must have templates folder.

2. Template Language

2.1 Variable

The role of the template is calculated and output variables, variable names must consist of letters, numbers, underscores (can not begin with an underscore) and points.

The syntax is as follows:

{{variable}}

When the template engine encounters point as book.title, it will be resolved in the following order:

1. Dictionary book [ 'title']

2. After the first attribute method, the book as an object, look for a property title, if not then find methods title ()

3. If the format is book.0 is resolved to a list of book [0]

If the variable does not exist, an empty string ''.

In the template you can not pass parameters when calling the method.

E.g:

{{book.btitile}}

First, the book as a dictionary, the btitle as a key name, a value book [ 'btitle'].

The book as an object, as the btitle property values ​​were book.btitle.

The book as an object, the object btitle as method, the value book.btitle.

E.g:

{{book.0}}

First, the book as a dictionary, 0 as the key name, a value book [0].

The book as a list, as the subscript 0, a book value [0].

If the resolution fails, filling the empty string template variables are used when output content.

When using a template variable, the difficulties ahead is a dictionary, it may be an object, it may also be a list.

2.2 Label

The syntax is as follows: 

%}% {Snippet

for label syntax is as follows:

% { For Item in List% }
Circular logic
{{Forloop.counter}} indicates that the current is the first few cycles, starting with 1
{%empty%}
This logic execution list is empty or does not exist
{%endfor%}

if the label syntax is as follows:

{%if ...%}
Logic 1
{ % Elif ...% }
Logic 2
{%else%}
Logic 3
{%endif%}

Comparison operators as follows:

Left and right end operator can not immediately variable or constant, must be a space. 

==
!=
<
>
<=
>=

Boolean operators are:

and
or
not

2.3 Filter

The syntax is as follows:

Pipe symbol | of applying a filter, for performing calculations, conversion operations, may be used in the variable tag.

If the filter parameter is required, colon: passing parameters.

Variable | Filter: Parameters

The length of the length, returns the number of characters contained in the string, or a list of tuples, the number of elements in the dictionary.

The default value of default, the default value is returned if the variable does not exist.

the Data | default: ' defaults '

DATE date, date type values ​​for string formatting, formatting characters used are as follows:

Y is the year, the format is 4, y is the year of two.

m represents month format 01,02,12 like.

d represents the date in the format of 01, 02 and so on.

j represents the date, format 1 and 2.

When denotes H, hexadecimal 24, h 12 hexadecimal representation.

i representing minutes, 0-59.

s for seconds, 0-59.

value | DATE: " the Y j dated on May H when m i s seconds min "

2.4 Notes

Use the following template in the template comments, this code will not be compiled, not output to the client; html comments can only comment html content, can not comment template language.

Single-line comment syntax is as follows:

{#...#}

Comments can contain any template code valid or invalid can be.

{# { % if foo % }bar{ % else % } #}

Multi-line comments using the comment label syntax is as follows:

{%comment%}
...
{ %}% Endcomment

3. template inheritance

Template inheritance and class inheritance meaning is the same, mainly to improve code reuse, reduce the workload of developers.

Typical applications: Web site header and footer.

3.1 parent template

If you find some of the same content in multiple templates, you should define this content into its parent template.

Tags block: a reserved area in the parent template, leaving sub-template filled with content differences, the name can not be the same. For better readability, it is recommended to endblock label with your name, the name and the name of the corresponding block of the same. Father templates also can use the data transfer from the context.

{% block name% }
Reserved area, you can write the default content, there is no default content
{ %}% Endblock name

3.2 sub-template

Tags extends: inheritance, writing in the first row of sub-template file.

The extends% { " parent template path " %}

Not all the sub-templates the reserved area is filled in the parent template, if the sub-template is not filled, the default value of the parent template definitions used.

Filling a reserved area designated parent template name.

{% block name% }
The actual content of the filling
{{Block.super}} for acquiring the content of the parent template block
{ %}% Endblock name

4. HTML escape

When the output templates for all text transfer, merge the following characters escaped automatically.

Less than <converted to & lt;

Greater than> converted to & gt;

Single quotation marks' convert & # 39;

Double quotes "is converted to & quot;
 & Convert symbol & amp;

Filters can be achieved escape html escaped variables, the default template will escape, usually omitted.

{{t1|escape}}

Filters safe: Local escaped, told template for this variable is safe and can be interpreted.

{{data|safe}}

Tags autoescape: setting a period of partial escape code, receiving on, off parameter.

{% autoescape% off }
...
{%endautoescape%}

A string of hard-coded default template will not be escaped, if you need to escape, that you need to manually escape.

Guess you like

Origin www.cnblogs.com/liuhui0308/p/12230442.html