Now that we can use variables in our templates, let’s look at different ways we can perform actions on them.
Filters are used by the template engine to act on template variables. To use them simply follow the variable with the filter name inside the delimiter and separate them with the |
character.
{{ variable | filter_name }}
The character |
separating the variable and the filter is called a pipe or vertical bar.
The filter title
acts on a string variable and capitalizes the first letter in every word. This is good for using as formatting on heading strings.
Given the variable assignment template_heading = "my very interesting website"
.
{{ template_heading | title }} OUTPUT My Very Interesting Website
Notice that the first letter of every word is now capitalized.
Filters can also take arguments. The default
filter will output the text in its argument when a variable isn’t passed to the template. Consider if no_template_variable
is missing from the render_template()
arguments.
{{ no_template_variable | default("I am not from a variable.") }} OUTPUT I am not from a variable.
The default
filter does not work on empty strings ""
or None
values. We will look at this scenario in the next exercise.
While filters perform more complex functions than simple operators, they are still small, focused actions. Here is a list of commonly applied filters and their descriptions. More information can be found in the Jinja2 documentation
title
: Capitalizes the first letter of each word in a string, known as titlecasecapitalize
: Capitalizes the first character of a string, such as in a sentencelower
/uppercase
: Makes all the characters in a string lowercase/uppercaseint
/float
: Changes any number variable to an integer/floatdefault
: Defines a default string if the variable is not definedlength
: Calculates the length of a string, list or dictionary variabledictsort
: Sorts a dictionary by its keys
Instructions
Since the recipe names are stored in all lowercase characters you need to capitalize the first letter of each word for the recipe heading.
In recipe.html apply a filter to template_recipe
that capitalizes the first letter of each word.
The description argument in app.py has been removed. Use a filter to make sure a proper description is displayed.
Use a filter on template_description
that will display text even if the variable isn’t passed through render_template
.
If you want to utilize the recipe name in your message, you can pass the filter a string such as:
"A " + template_recipe + " recipe."
Once you run the code go to app.py and put template_description
variable assignment back in the render_template()
function of the recipe route.
In the ingredients heading it might be important to display the number of ingredients. This could help a prospective chef know how complicated a recipe is.
In recipe.html create a separate delimiter block at the end of, but still inside, the ingredients heading. Inside this block display the length of template_ingredients
.
For example the heading output can look like this: Ingredients - 3
In the instructions section the dictionary has unsorted keys.
Use a filter to make sure the dictionary is output in sorted order.