Previous Page Next Page

Implementing if Logic

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.

Using if

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 %}

Using ifchanged

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 %}


Using ifequal

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 %}

Using ifnotequal

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 Content

In 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:

1.
Open the iFriends/templates/People/person_details.html file in an editor.

2.
Remove the following line of code from the file:

<li>Gender: {{ p.gender }}</li>

3.
Add the following ifequal tag code to the file to compare the value of gender to the string M. Then use the Australian terms Bloke and Sheila instead of M and F, as shown in Listing 8.3:

<li>Gender:
     {% ifequal p.gender "M" %}
         Bloke
     {% else %}
         Sheila
     {% endifequal %}
</li>

4.
Save the file.

5.
Access the details() view of the People object in a web browser to verify that the view now displays Bloke and Sheila correctly, as shown in Figure 8.2.

Figure 8.2. Web page generated by the details() view of the People application.


Listing 8.3. Full Contents of iFriends/templates/People/person_details.html

{% extends "iFriends_base.html" %}

{% block title %}Details{% endblock %}
{% block content %}
<table width=100%>
<tr bgcolor="aabbcc"><td colspan="3">
<font size="5" color="white">Personal Information</font></td></tr>
<tr valign="top"><td width=30% bgcolor="aaaaaa"><font color="white" size="5">
    <li>Name: {{p.name}}</li>
    <li>Birthday: {{ p.birthday }}</li>
    <li>Gender:
        {% ifequal p.gender "M" %}
            Bloke
        {% else %}
            Sheila
        {% endifequal %}
    </li>
    <li>Desc: {{ p.desc }} </li>
</font></td>
<td width=40% bgcolor="aa99aa"><font color="white" size="4">
    {% include "quote.html" %}
</font></td>
<td width=30%>
    <h3>Contact Info</h3>
    <li>Email: {{ p.email }}</li>
    <li>Website: {{ p.favoriteURL }}</li>
</td></tr>
{% endblock %}


					  


Previous Page Next Page