0Pricing
Flask Academy · Lesson

Jinja2 Loops and Conditionals

Render lists and branch markup in templates.

Jinja2 Loops and Conditionals is a free Flask Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Markup That Reacts

Sometimes you must repeat rows or hide a section. Jinja2 adds logic tags so your template can loop and branch over data.

The Statement Delimiter

Logic uses {% %}, not double braces. Braces output a value, while percent tags run control flow like loops and conditions.

A Basic for Loop

Repeat markup with a for loop. Each pass binds the current item so you can print it inside the loop body.

{% for item in items %}
  <li>{{ item }}</li>
{% endfor %}

Loops Must Close

Every block opens and closes. A for loop ends with endfor, which tells Jinja2 exactly where the repeated section stops.

The loop Variable

Inside a loop, Jinja2 gives you a special loop object. Use loop.index for a 1-based counter on each pass.

{% for u in users %}
  {{ loop.index }}. {{ u }}
{% endfor %}

Handling Empty Lists

A for block can include an else branch. It runs only when the list is empty, perfect for a no items message.

{% for t in todos %}{{ t }}{% else %}None yet{% endfor %}

An if Condition

Show markup only when a test passes using if. It ends with endif and accepts ordinary Python-style comparisons.

{% if user %}
  <p>Welcome back</p>
{% endif %}

Adding else

Pair an if with else to handle the other case. One branch renders when true, the other when the test is false.

{% if user %}Hi{% else %}Please log in{% endif %}

Multiple Branches with elif

Chain extra cases with elif. Jinja2 checks each in order and renders the first branch whose test is true.

{% if n > 0 %}+{% elif n < 0 %}-{% else %}0{% endif %}

Combining Loops and Ifs

Nest an if inside a loop to filter as you go. This lets each item decide whether it appears in the output.

{% for p in posts %}{% if p.live %}{{ p.title }}{% endif %}{% endfor %}

Keep Templates Readable

Light logic in templates is fine, but deep nesting hurts. Prepare filtered data in your view to keep markup clean.

Quick Check

Which delimiter does Jinja2 use for control flow like loops and conditionals?

Recap

You can now loop with for, branch with if and elif, use the loop counter, and handle empty lists, all inside templates. Well done! ✨

Frequently asked questions

Is the “Jinja2 Loops and Conditionals” lesson free?

Yes — the full text of “Jinja2 Loops and Conditionals” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Jinja2 Loops and Conditionals”?

Render lists and branch markup in templates. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Jinja2 Loops and Conditionals” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. render_template and the templates Folder
  2. Pass Data into a Template
  3. Jinja2 Loops and Conditionals
  4. Autoescaping and the safe Filter
← Back to Flask Academy