An Essential Guide to Jinja Literals
n
nn
Strings
nStrings are used for text and are defined by enclosing text in either single (') or double (") quotes. They are versatile and are commonly used for:n
- n
- Passing arguments to filters and functions:n
{{ "hello world"|upper }}n
- Including text directly in a template:n
<h1>My Website</h1>n
- As values in data structures:n
['home', 'about', 'contact']n
n
n
n
nn
nn
Numbers
nJinja supports both integers (whole numbers) and floating-point numbers (numbers with a decimal part).n
- n
- Integers: These are whole numbers without a decimal point. For readability, you can use an underscore (
_) to separate groups of digits, similar to how you might use a comma in a standard number.n{{ 42 }}n{{ 1_000_000 }}n
- Floating-point numbers: These numbers contain a decimal point. You can also use scientific notation with
eorEto represent the exponent. Underscores can be used for legibility here as well.n{{ 3.14 }}n{{ 6.022_140_76e23 }}n
n
n
nNumbers are crucial for calculations, comparisons, and defining quantities within your templates.nn
nn
Collections (Lists and Tuples)
nJinja allows you to define collections of objects directly in your templates. These are useful for storing sequential data that you need to iterate over or reference.n
- n
- Lists (
[]): Lists are defined by square brackets and can be modified. They are perfect for a list of items that you want to loop through.n{% for fruit in ['apple', 'banana', 'orange'] %}n <li>{{ fruit }}</li>n{% endfor %}n
- Tuples (
()): Tuples are similar to lists but are immutable, meaning they cannot be changed after they are created. If a tuple contains only one item, it must be followed by a comma to distinguish it from a simple expression in parentheses, e.g.,('single_item', ). Tuples are often used to group related data together. A common pattern is to use a list of tuples to create a navigation menu, as shown in the following example:n<ul>n{% for href, caption in [('index.html', 'Home'), ('about.html', 'About'), ('contact.html', 'Contact')] %}n <li><a href="{{ href }}">{{ caption }}</a></li>n{% endfor %}n</ul>n
n
n
nThis approach allows you to iterate over a collection of paired values efficiently.nn
nn
Dictionaries ({})
nA dictionary, or dict, is a collection of key-value pairs. Each key must be unique and is used to retrieve its corresponding value. Dictionaries are less common as literals directly in templates but are very important for representing structured data. You might use them to pass keyword arguments or to configure filters like xmlattr().n
{% set user = {'name': 'Alice', 'age': 30} %}n<p>User name: {{ user.name }}</p>
nn
nn
Booleans (true and false) and none
nBoolean literals represent truth values and are fundamental for control flow and conditional logic.n
- n
trueandfalse: These represent booleanTrueandFalse. They are used inifstatements and logical expressions to determine which parts of a template to render.n{% if is_admin %}n <p>Welcome, admin!</p>n{% endif %}n
none: Thenoneliteral represents the absence of a value, similar to Python’sNone.
n
n
nWhile these constants can also be written in title case (True, False, None), Jinja’s best practice is to use the lowercase versions for consistency with other Jinja identifiers.nn
nn
Conclusion
nJinja literals are the bedrock of template creation. By mastering strings, numbers, collections, and booleans, you gain the ability to embed both static and dynamic data directly into your templates. These simple yet powerful building blocks allow you to create dynamic content, control logic, and format data in a clear and efficient manner, making them an indispensable part of your Jinja toolkit.nn
