Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Loops · Templates

Direct answer

Loops in templates repeat a block for each item, such as {% for intf in interfaces %} to generate interface sections, keeping templates short for many items.

When to use this

Use this when repeating blocks in templates.

First 30 seconds

1. Loops render repeated sections (interfaces, VLANs).

2. Sort data for stable output.

3. Handle empty lists.

4. Avoid complex logic inside loops.

5. Test with multiple items.

Operational verification

list → loop → repeated blocks

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
template rendering automation
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

Platformtemplate rendering automation · Automation implementations are tool and platform specific. Use the adjacent Domain for the technology being automated and this Domain for automation behavior.
Scope / prerequisiteUse this when repeating blocks in templates.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 Verification path Verify boundary

The negative fixture should deliberately exercise risks such as undefined values, unsafe escaping, implicit type conversion, environment-specific filters, secret exposure, whitespace-sensitive syntax and valid render producing invalid target configuration, choosing the cases most relevant to iteration input set, ordering, per-item status, break/continue/failure behavior and safe handling of partial progress.

Step 2 Common mistakes Narrow the fault
  • Loops generating duplicate config lines.
Evidence references

Evidence · 1 on this Topic

Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.

observation · 1
Key points
  • {% for %} repeats blocks.
  • loop.index and loop.last help formatting.
  • Handle empty lists.
Key checks
  • {% for vlan in vlans %}…{% endfor %} — loop
  • loop.index / loop.last usage — formatting
  • Empty list handling ({% else %}) — edge cases
ReferenceImportant terms & boundaries1 section · depth on demand

In depth

Loops in templates generate repeated blocks from lists — one stanza per VLAN, interface or neighbour. Jinja2 loops also provide a loop object with position information.

{% for intf in interfaces | sort(attribute='name') %}
interface {{ intf.name }}
 description {{ intf.description | default('unused') }}
{% if intf.mode == 'access' %}
 switchport access vlan {{ intf.vlan }}
{% endif %}
{% if not loop.last %}!{% endif %}
{% else %}
! no interfaces defined
{% endfor %}
VariableMeaning
loop.index / loop.index0Position (1- / 0-based)
loop.first / loop.lastFirst/last iteration
loop.lengthNumber of items

Sort lists before looping so rendered output is deterministic; otherwise diffs show changes where nothing changed.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor doctemplate rendering automation current

Authoritative/upstream documentation reviewed for bounded claims in Templates > Loops. Documentation verification only; this is not observed lab or production Evidence.

Basis: https://jinja.palletsprojects.com/en/stable/templates/ · confidence 94%
Depth when you need it

Deep reference

Healthy vs suspicious evidence

Output interpretation

For iteration input set, ordering, per-item status, break/continue/failure behavior and safe handling of partial progress, prefer typed fields and stable IDs over human presentation text.

Confirmation

Confirm action

Continue?