Django also provides rudimentary if logic to the template parser. You can use the if, ifchanged, ifequal, and ifnotequal tags to determine how and if content should be displayed. The following sections describe the different types of if tags and their uses.
The if tag evaluates a variable and determines if it is true. If the variable evaluates to true, the template engine renders the code inside the if block. You need to add an endif tag to end the block. You can also use the optional else tag with all the if tags to provide alternative template code.
For example, suppose your template is expecting a list of cars called carList. You could use the following code snippet to determine if the car list is empty and note that in the web page, instead of just having a blank section in the web page:
{% if carList %} {% for car in carList %} <li>{{ car.model }}</li> {% endfor %} (% else %} No cars in list. {% endif %}
You can use a not operator in the if tags to change the if logic. For example, the preceding code snippet could also be written as follows:
{% if not carList %} No cars in list. (% else %} {% for car in carList %} <li>{{ car.model }}</li> {% endfor %} {% endif %}
You can also use or and and operators in the if tags to chain together logic. For example, if you want to display a list only if both teacherList and studentList have entries, you could use the following code snippet:
{% if studentList and teacherList %} <h2>Students</h2> {% for s in studentList %} <li>{{ student }}</li> {% endfor %} <h2>Students</h2> {% for s in studentList %} <li>{{ student }}</li> {% endfor %} {% endif %}
The ifchanged tag is used inside for loops to determine if a variable value has changed since the last iteration. If the value has changed, the template engine renders the code inside the ifchanged block. You need to add an endifchanged tag to end the block.
For example, if your template is parsing blog entries that contain a date field, you could use the following code to note the change in dates:
{% for b in blogList %} {% ifchanged b.date.date %} <h2>Date: {{ b.date.date }} </h2> {% endifchanged %} <h1>{{ b.title }}</h1> {{ b.text }} {% endfor %}
Did you Know?
The ifchanged tag can also determine if its own rendered contents have changed. Instead of specifying variable(s) inside the tag statement, you just need to add the variable(s) inside the ifchanged block. If they change, the content changes from the last state and is rendered again by the template engine. For example:
{% ifchanged %}<h3>{{ b.date }}</h3> {% endifchanged %}
The ifequal tag determines if two arguments are equal. The arguments can be either variables or hard-coded strings. If the arguments match, the template engine renders the code inside the ifequal block. You need to add an endifequal tag to end the block.
For example, if your template is parsing a list of users, and you want only users who have a last name of Arthur, you could use the following code snippet:
{% for user in userList %} <h1>Arthurs</h1> {% ifequal user.last "Arthur" %} <li>{{ user.first }}</li> {% endifequal %} {% endfor %}
The ifnotequal tag determines if two arguments are not equal. The arguments can be either variables or hard-coded strings. If the arguments do not match, the template engine renders the code inside the ifnotequal block. You need to add an endifnotequal tag to end the block.
For example, if your template is parsing a list of blogs, and you want to exclude blogs with the title of "test," you could use the following code snippet:
{% for blog in blogList %} {% ifnotequal blog.title "test" %} <h1>{{ blog.title }}</h1> {{ blog.text }} {% endifnotequal %} {% endfor %}
Try It Yourself: Use if Logic to Determine the View ContentIn this section, you will use ifequal logic to control the content displayed by the person_details.html template. Follow these steps to modify the person_details.html template file so that it can determine if the person's gender is M or F. Add some character to the site by displaying the Australian terms "Bloke" and "Sheila" instead of M and F:
Listing 8.3. Full Contents of iFriends/templates/People/person_details.html
|