Menu
Jinja If Expressions: concise conditional expressions – Jinja Documentation

Jinja If Expressions: concise conditional expressions

Using Jinja'sifExpression

Jinja's

expression, also known as a ternary operator, offers a concise way to handle conditional logic on a single line. Instead of using a multi-line

statement block, you can use an

expression to choose between two values based on a condition. This is particularly useful for tasks like setting a variable, extending a template, or conditionally rendering a small piece of text. It follows a simple, readable syntax:

Basic Syntax and Structure

The fundamental structure of an

expression is straightforward:

text
{{ expression_if_true if condition else expression_if_false }}

Jinja evaluates the

first.

  • If theconditionis "truthy" (e.g.,true, a non-empty string, a non-zero number), the expression returnsexpression_if_true.
  • If theconditionis "falsy" (e.g.,false, an empty string,0,none), the expression returnsexpression_if_false.

This is a single-line equivalent to a full

block:

text
{% if condition %}
    {{ expression_if_true }}
{% else %}
    {{ expression_if_false }}
{% endif %}

expression is a cleaner and more compact way to handle simple choices, making your templates more readable.

Practical Applications

expression is a versatile tool for various templating tasks.

1. Conditional Template Inheritance

One of the most powerful uses is in the

tag. You can dynamically choose which parent template to extend based on a variable.

text
{% extends layout_template if layout_template is defined else 'default.html' %}

In this example, Jinja checks if the variable

has been defined. If it has, the current template extends that layout. If not, it falls back to a default layout,

. This prevents errors if a specific layout isn't provided and ensures a consistent base for your pages.

2. Dynamic Variable Assignment

You can use an

expression to set a variable to one of two values.

text
{% set button_class = 'btn-primary' if user.is_logged_in else 'btn-secondary' %}
<a href="#" class="{{ button_class }}" data-translate="true">Click here</a>

This snippet checks if the user is logged in. The

is set to

if they are, and

if they aren't. This allows you to apply different CSS styles based on a condition with minimal code.

3. Inline Text Rendering

expression is excellent for rendering a small piece of text or a simple value based on a condition, right within a larger string.

text
<p data-translate="true">You have {{ cart.item_count }} item{{ 's' if cart.item_count != 1 }} in your cart.</p>

This example correctly pluralizes the word "item." If

is anything other than 1, it adds an "s" to the end of the word.

The OptionalelsePart

part of the

expression is optional. When you omit it, and the condition is

, the expression will implicitly evaluate to an

object.

text
{{ "[{}]".format(page.title) if page.title }}

In this case, if

is a truthy value (e.g.,

), the output will be

. However, if

is falsy (e.g., an empty string or

), the expression returns

. When

is printed, it typically results in an empty string, so no output is rendered. This is a common pattern for conditionally including a value without needing to specify a fallback.

Conclusion

Jinja's

expression is a powerful and concise tool for embedding conditional logic directly into your templates. By using the

expression, you can write more readable and compact code for tasks like dynamic inheritance, variable assignment, and inline text modifications. While it's best suited for simple conditions, its ability to streamline your template logic makes it an indispensable part of your Jinja toolkit. For complex or multi-step logic, a full

block is still the better choice, but for quick, one-off decisions, the

expression is the ideal solution.