The Forloop of Django templates

Source: Internet
Author: User

In the Django template, there is the Forloop template variable, quite like the foreach.customers in PHP smarty,

Smarty foreach is as follows:

{foreach name=customers from= $custid item=curr_id}
{$smarty. foreach.customers.iteration} <--tells you which item to are at (integer)
{$smarty. Foreach.customers.first} <--tells if the current item is the
{$smarty. Foreach.customers.last} <--tells if the current item is the last item (Boolean)
{$smarty. Foreach.customers.total} <--tells you to many items are in the array (integer)
{/foreach}


And the Django Forloop is as follows:
There is a template variable called ' forloop ' in each ' {% for%} ' loop. This variable has properties that indicate the circular progress information.

Forloop.counter is always an integer counter that represents the number of executions of the current loop. This counter starts at 1, so the Forloop.counter will be set to 1 on the first loop.
{% for item in todo_list%}
<p>{{Forloop.counter}}: {{item}}</p>
{% ENDFOR%}
Forloop.counter0 is similar to Forloop.counter, but it is counted from 0. This variable is set to 0 the first time the loop is executed.
Forloop.revcounter is an integer variable that represents the remaining items in a loop. Forloop.revcounter will be set to the total number of items in the sequence when the loop is first executed. In the last loop execution, this variable will be set to 1.
Forloop.revcounter0 is similar to Forloop.revcounter, but it is an end index of 0. 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, and if the iteration is the first execution, then it is set to "' in the following case, this variable is useful:
System MESSAGE:WARNING/2 (&LT;STRING&GT;, line 1071); Backlink
Inline literal start-string without end-string.
{% for object in objects%}
{% if Forloop.first%}<li class= ' i ' >{% else%}<li>{%%}
{{Object}}
</li>
{% ENDFOR%}
Forloop.last is a Boolean value that was set to true the last time the loop was executed. A common use is to place a pipe character between a series of links (|)
{% for link in links%} {{link}}} {% if not forloop.last%} | {% ENDIF%} {% ENDFOR%}
The template above may produce the following results:
Link1 | Link2 | Link3 | Link4
Another common use is to add commas to each word in the list.
Favorite Places:
{% for P in places%} {{p}}} {% if not forloop.last%}, {% endif%}{% endfor%}
Forloop.parentloop is a reference (in the case of a nested loop) to a Forloop object that points to the upper loop of the current loop. Examples here:
{% for country in countries%}
<table>
{% for City in country.city_list%}
<tr>
<td>country #{{Forloop.parentloop.counter}}</td>
<td>city #{{Forloop.counter}}</td>
<td>{{City}}</td>
</tr>
{% ENDFOR%}
</table>
{% ENDFOR%}
Forloop variables can only be used in loops. Forloop is inaccessible after the template parser encounters the {% endfor%} label.

Context and Forloop variables
In a {% for%} block, existing variables are removed to prevent forloop variables from being overwritten. Django will move this variable into the forloop.parentloop. Normally we don't have to worry about this, but once we've defined the Forloop variable in the template (we're against it, of course), it will be renamed in the {% for%} block in Forloop.parentloop.

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.