Jinja2 Operators: . and [] (Dot and Bracket)
n
. (dot) and [] (bracket) operators in Jinja2 are used to access attributes of an object or items in a sequence/mapping. They are the primary way to drill down into complex data structures like objects, lists, and dictionaries. Both operators serve a similar purpose but have distinct use cases and syntax.nn
nn
How They Work
n
- n
- Dot (
.) operator: This is the most common way to access an attribute on an object. For example,user.namewould access thenameattribute of theuserobject. It can also be used to access dictionary keys, making it a concise alternative to the bracket operator for simple keys. - Bracket (
[]) operator: This operator is more versatile. It is used to:n- n
- Access items in a list or tuple by their numerical index:
products[0]. - Access values in a dictionary by their key:
user['email']. - Access dynamic keys in a dictionary or attributes on an object, where the key name is stored in another variable:
user[key_variable]. This is its key advantage over the dot operator.
n
n
n
n
- Access items in a list or tuple by their numerical index:
n
n
nn
nn
Demonstration with Code Samples
n
1. Accessing Object Attributes and Dictionary Keys
nThis example shows the most basic use of both operators for accessing data.nnJinja2 Templaten
<p>User Name: {{ user.name }}</p>n<p>User Email: {{ user['email'] }}</p>nn<p>First Product: {{ products[0].name }}</p>
nExplanation: The template first uses the dot operator to get the name attribute from the user object. It then uses the bracket operator to get the email key from the same object (assuming it’s a dictionary-like object). Finally, it accesses the first item in the products list using a numerical index and then uses the dot operator to get its name attribute.nn
nn
2. Using Dynamic Keys with the Bracket Operator
nThis is where the bracket operator shines. You can use a variable inside the brackets to access a key or attribute, which is impossible with the dot operator.nnJinja2 Templaten
{% set user_info_key = 'last_login' %}n<p>Last Login: {{ user[user_info_key] }}</p>nn{% for key, value in user_info.items() %}n <p>{{ key|capitalize }}: {{ value }}</p>n{% endfor %}
nExplanation: In the first part, the key last_login is stored in the user_info_key variable, which is then used to dynamically access the value from the user dictionary. In the second part, a for loop iterates over the keys and values of a dictionary, using the key variable inside the brackets to access the value, which is not possible with the dot operator.nn
nn
3. Accessing Nested Data
nBoth operators can be chained to access deeply nested data structures.nnJinja2 Templaten
<p>First order's first product: {{ user.orders[0].products[0].name }}</p>
nExplanation: This single line of code chains a series of dot and bracket operators to navigate a complex data structure. It starts with the user object, gets the orders list, accesses the first item in that list, gets the products list from that item, accesses the first product, and finally gets its name attribute.nn
