Previous Page Next Page

Adding Cycle Logic to Tables

The cycle tag is a useful tool if you need to modify values as you cycle through a list. The cycle tag accepts a comma-separated list of arguments that will be cycled through.

For example, if you were creating a large table in your template and you wanted each row of the template to use a different background color, you could use the cycle tag to set the bgcolor value of the rows. The following code generates the web page shown in Figure 8.5:

<table width=100%>
{% for line in report %}
    {% if forloop.first %}
    <tr bgcolor="aaaaaa">
    {% else %}
    <tr bgcolor="{% cycle eeeeee,cccccc %}">
    {% endif %}
    {% for item in line %}
        <td>{{ item }}</td>
    {% endfor %}
    </tr>
{% endfor %}
</table>

Figure 8.5. Table generated with alternating dark and light lines using the cycle tag.


Previous Page Next Page