This section discusses using filters to create and manipulate lists inside the template files. You probably will work quite a bit with lists inside the templates, and you won't have the control that you normally would inside Python code. However, the template engine does provide some much-needed filters to help you manage lists.
The following sections describe the slice, make_list, join, and random filters and how to apply them to lists.
The slice filter accepts an argument containing Python list slicing syntax. Depending on the argument's value, it renders a subset of the original list.
This is useful if you need to limit the number of entries from the list that you want to render to the HTML output. For example, if a template is used to render items returned from a search, you could use the slice filter in the following line of code to filter only the top ten:
{{ searchResults|slice":10" }}
Just as in Python, the number on the left of the : refers to the starting index, and the number on the right refers to the ending index. If no value is specified for the starting index, the slice filter starts at the first item. If no value is specified for the ending index, the slice filter stops after the last item.
Also, just like Python, the index is 0-based, and negative numbers access items from the end of the list. If you wanted the template in the preceding example to retrieve the last 10 items in the search routine, you would use the following syntax:
{{ searchResults|slice"-10:" }}
The slice filter can be applied to a string variable as well. For example, if you have a string, str, with a value of Hello World, you could use the slice filter in the following code to display only Hello:
{{ str|slice:":5" }}
The make_list filter renders the variable as a list. The make_list filter accepts no arguments. If the variable is numeric, the make_list filter renders a list of digits. If the variable is a string, it renders a list of characters.
The following example shows the result of applying the make_list filter to a string, s, with a value of Python:
{{ s|make_list }}
It renders the following:
['P','y','t','h','o','n']
The following example shows the result of applying the make_list filter to a number, n, with a value of 2008:
{{ n|make_list }}
It renders the following:
['2','0','0','8']
The make_list filter can be used in many different ways, depending on how creative you get. For example, you can split a large number into a list of characters and then process each digit separately.
The join filter works similarly to the Python str.join(list) function. The join filter accepts a string object as the only argument. The entries in the list are joined, with the value of the string argument placed between each entry and rendered as a string.
The join filter is useful if you need to display the elements of a list in comma- or space-separated form. For example, if you simply want to display names in a person list, you could use the following code in a template:
{% filter linebreaks %} There are {{ people.length }} people in the list. Their names are : {{ people|join:", " }}. {% endfilter %}
The rendered output from this filter would look something like this:
There are 5 people in the list. Their names are: Arthur, Lancelot, Galahad, Robin, Tim.
Did you Know?
The join filter can be applied to a string variable as well. For example, if you have a string, s, with a value of Title, you could use the join filter as follows to display T-i-t-l-e:
{{ s|join:"-" }}
The random filter selects a random item from a list. This can be useful for keeping web content from getting too stale. For example, if you use a quote of the day on your website, you could pass a list of quotes, and the random filter would allow you to generate a random quote each time the web page is accessed.
Here's the syntax for using the random filter:
{{ QuoteOfTheDay|random }}
Try It Yourself: Apply a Filter to a List ObjectIn this section, you will modify the quote.html template to select a random quote from the list. Currently the quote is hard-coded in, so you need to add code to the details view function to send the quote as part of the context dictionary. By the Way In this section, you will stop the development server and create a new application called Quotes. After the application is created, you will add some quotes to the database using the admin interface. If you've forgotten how to do some of these tasks, refer to Hours 2 and 3. Following these steps to randomize the quote displayed on the person details view:
Listing 9.2. Full Contents of iFriends/Quotes/models.py
Listing 9.3. Full Contents of iFriends/People/views.py
Listing 9.4. Full Contents of iFriends/templates/quote.html
|