Jinja is a templating engine for Python, used primarily in web development to create dynamic HTML pages, though it can also be used for other templating needs. It allows you to use expressions, variables, and control structures in templates, which are rendered with data provided by Python code.
1. Lists and Python Functions
- Python list: A list is a collection of items in Python, which can be of different types. For example:
my_list = [1, 2, 3, 4] len(list): This Python function returns the number of elements in a list.len(my_list) # Returns 4
2. Jinja Filters
In Jinja, filters are used to transform data in templates. You can pass data through filters using the pipe | operator. They are similar to functions in that they take an input, modify it, and return an output.
list | length: This filter in Jinja is equivalent to Python'slen()function and returns the length of the list.
{{ my_list | length }} # Outputs 4 if my_list is [1, 2, 3, 4]
3. Functions or Methods
- Functions: In both Jinja and Python, a function is a block of code designed to perform a particular task. You can define your own functions in Python or use built-in functions.
- Methods: These are functions that belong to an object. For example, the
append()method is a function of Python's list object.my_list.append(5) # Adds 5 to my_list
4. groupby("producer"): Grouping in Jinja
The groupby filter in Jinja allows you to group a list of dictionaries by a common key, such as producer in this case. This filter works similarly to SQL's GROUP BY clause. It groups data based on a shared value and returns a grouper object.
Example:
Assume you have a list of dictionaries representing movies, each having a producer key:
{% set movies = [
{"title": "Movie A", "producer": "Producer 1"},
{"title": "Movie B", "producer": "Producer 2"},
{"title": "Movie C", "producer": "Producer 1"},
{"title": "Movie D", "producer": "Producer 2"}
] %}
You can group the list by producer using groupby:
{% for producer, group in movies | groupby("producer") %}
<h2>{{ producer }}</h2>
<ul>
{% for movie in group %}
<li>{{ movie.title }}</li>
{% endfor %}
</ul>
{% endfor %}
Output:
Producer 1
- Movie A
- Movie C
Producer 2
- Movie B
- Movie D
- Grouper: In this context, the
grouperrefers to the value by which the data is grouped, such as"Producer 1","Producer 2", etc. - Data associated with the grouper: The grouped items (movies) that share the same value for the specified key (
producer) will be listed under each corresponding grouper.
Summary:
- Jinja Filters: Modify data in templates (e.g.,
list | length). - Python
len(list): Returns the length of the list. groupby("producer"): Groups items in a list by a specified key (e.g.,producer) and provides a way to iterate over the grouped data.