Django Template system-Template language 1

Source: Internet
Author: User

This article describes several common tags:

1.if Tags: {% if%} ... {% ENDIF%}

2.for Tags: {% for%} ... {% ENDFOR%}

3. Compare Tags: {% ifequal/ifnotequal%} ... {% endifequal/endifnotequal%}

4. Note Label: {% comment%} ... {% Endcomment%}

Template

is a plain text file, or a plain Python string that has been tagged with the Django template language. Templates can contain template tags and variables .

Template Label is the role of marking in a template, also known as the Block tag. is surrounded by {% and%}, shaped like: {% * * *%}

variables is a tag that is used to output values in a template. the variable label is surrounded by { { and } } and is shaped like this: {{varname}}

Generic tags and filters in the template language

If, else label: {% if * *%} ... {% Else%} ... {% ENDIF%}

If you can accept and , or or not keyword to judge multiple variables, it is no problem to use the same logical operator multiple times, such as: {% if * or * or *%} is legal; but we can't put different combinations, such as: {% if * or * and *%} are illegal;

{% Else%} is an optional label;

There is no {% elif %} tag, you can use a nested {% if%} tag to achieve the same effect;

The system does not support the use of parentheses to combine comparison operations. If you need to use parentheses to compose your logic, consider moving it outside of the template, and then pass in the result as a template variable, or replace it with a nested {% if %} tag;

{% if * *%}

...

{% Else%}

...

{% ENDIF%}

For label: {% for x in y%} ... {% empty%} ... {% ENDFOR%}

{% for%} allows iterating on a sequence similar to the case for a python for statement. Add a reversed to the label so that the list is reversed to iterate {% for x in y reversed %};

{% empty%} is an optional label that allows you to define what the output will be when the list is empty. The same effect can be achieved by detecting whether the sequence is empty before the loop with the If label;

This loop does not support exit break, nor does it support continue;

{% for%} There is a template variable called forloop in the loop, which has a genus that indicates the loop progress information. The forloop variable can only be used in loops, andForloop is inaccessible after the template parser encounters the {% endfor %} tag.

forloop.counter is an integer counter that represents the number of executions of the current loop, which starts at 1, so forloop.counter will be set to 1 on the first loop

Forloop.counter0 is similar to forloop.counter , but it is counted from 0, and this variable is set to 0 when the first loop is executed.

forloop.revcounter is an integer variable that represents the remainder of the loop. At the first execution of the loop, Forloop.revcounter will be set to the total number of items in the sequence, and this variable will be placed 1 when the last loop is executed.

Forloop.revcounter0 is similar to forloop.revcounter , but it takes 0 as the end index. The first time the loop is executed, the variable is set to the number of items in the sequence minus 1

Forloop.first is a Boolean value that is set to true if the iteration is executed for the first time

Forloop.last is a Boolean value that is set to True when the last loop is executed

Forloop.parentloop is a reference to the Forloop object in the previous loop of the current loop (in the case of nested loops)

ifequal, ifnotequal tags: {% ifequal var1 var2%} ... {% Else%} ... {% endifequal%}, {% ifnotequal var1 var2%} ... {% Else%} ... {% ifnotequal%}

              {% ifequal %}   label compare two values When they are equal, shown in  {% ifequal  %}   and  {% endifequal  %} All values in  ;

{% ifnotequal %} label compares two values when they are not equal, displayed in {% ifnotequal %} and {% endifnotequal %} ;

Support for optional {% else%} tags;

{% ifequal/ifontequal %} the parameter label parameters are only template variables, strings, integers and decimals, any other type, such as the dictionary type of Python, List type, Boolean type, cannot be used in {% ifequal/ifnotequal %} .

Comment Label: {# ... #} {% comment%} ... {% endcomment%}

The contents of the comment are not output when the template is rendered.

{# ... #} annotations with this syntax cannot span multiple lines, and this restriction is intended to improve the performance of template parsing;

{% comment%} template tag implements multiline annotation;

Django Template System-Template language 1

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.