DevOps Bootcamp · Lesson

Loops & if Blocks in Templates

Generate repeated config sections.

Lesson 3 of 413 steps

Loops & if Blocks in Templates is a free DevOps Bootcamp lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, and your progress syncs across the web and the CoddyKit app. The DevOps Bootcamp course includes 4 lessons in total.

Logic Lives in {% %}

Beyond printing values, Jinja2 runs control logic inside {% %} tags. This is how one template generates many lines. 🔁

A Basic for Loop

Use a for loop to repeat lines over a list. Every item produces another block of rendered text.

{% for host in backends %}
server {{ host }};
{% endfor %}

Always Close Your Blocks

Each opening tag needs a closing one: endfor ends a loop, just as endif ends a condition. Forgetting it breaks rendering.

Loop Over a Dictionary

Walk a dictionary's pairs with .items(), capturing each key and value to render structured config.

{% for key, val in settings.items() %}
{{ key }} = {{ val }}
{% endfor %}

The loop Variable

Inside a loop, Jinja2 gives you loop.index for the current count and loop.first or loop.last as handy booleans.

{% for s in servers %}
server{{ loop.index }} {{ s }};
{% endfor %}

A Simple if Block

An if block renders its body only when the condition is true, so optional config appears only when it should.

{% if enable_ssl %}
ssl on;
{% endif %}

if, elif and else

Chain choices with elif and a final else. Exactly one branch renders, just like in Python.

{% if env == 'prod' %}
level = warn
{% else %}
level = debug
{% endif %}

Conditions Inside Loops

Nest an if inside a for to render only matching items, like skipping disabled servers in a pool.

{% for h in hosts %}
{% if h.enabled %}
server {{ h.ip }};
{% endif %}
{% endfor %}

Whitespace Control

Loops can leave blank lines behind. A dash like {%- -%} trims surrounding whitespace for tidy output.

{%- for h in hosts -%}
{{ h }}
{%- endfor -%}

The else of a for Loop

A for can have an else that runs only when the list was empty, perfect for a sensible fallback line.

{% for h in backends %}
server {{ h }};
{% else %}
# no backends defined
{% endfor %}

Print vs Logic Tags

Keep them straight: {{ }} outputs a value, while {% %} controls flow but prints nothing itself.

Quick Check

You want to repeat a config line for every host in a list. Which tag starts that loop?

Recap

You can now loop with for, branch with if, read loop.index, nest them and trim whitespace. One template now writes dynamic config. ✨

Free to start

Learn DevOps Bootcamp with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
142
Lessons
568

Frequently Asked Questions

Is the “Loops & if Blocks in Templates” lesson free?

Yes — any 3 lessons of the DevOps Bootcamp learning path, including “Loops & if Blocks in Templates”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The DevOps Bootcamp course includes 4 lessons in total.

What will I learn in “Loops & if Blocks in Templates”?

Generate repeated config sections. You practise DevOps Bootcamp 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 DevOps Bootcamp?

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

How long does the “Loops & if Blocks in Templates” 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 DevOps Bootcamp lesson?

Yes. Every DevOps Bootcamp 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. The template Module & .j2 Files
  2. Variables & Expressions in Jinja2
  3. Loops & if Blocks in Templates
  4. Filters: default, join & upper
← Back to DevOps Bootcamp