Jinja2 for loop generates repeating config

300-435 By islever Editorial Last updated

Construct advanced network configuration templates using Jinja2 constructs such as loops, conditionals, output modifiers, and filters

Short answer

A A Jinja2 for loop iterates a list to generate repeating configuration blocks, as in {% for item in items %} ... {% endfor %}

Question

Jinja2 for loop usage?
  1. Iterates list to generate repeating config Correct Answer
  2. Conditional logic
  3. Variable assign
  4. Filter

Key Takeaways

A Jinja2 for loop iterates over a list in the template context and emits its body once per element, producing repeated configuration stanzas such as interface blocks. Option A is correct because {% for item in items %} is the construct that repeats output, while conditional logic ({% if %}), variable assignment ({% set %}), and filters (pipe operators) each perform a different, non-repeating transformation.

Expert Analysis

{% for item in items %} generates repeated config blocks.

Option A is the only choice that describes the purpose of a Jinja2 for tag. The construct {% for item in items %}... {% endfor %} walks every element of a sequence — an interface list, a VLAN list, a prefix list — and emits the block body once per element, so a single template produces a full repeating configuration stanza for each entry. That is exactly what "Iterates list to generate repeating config" says, and it is the mechanism the reference identifies.

Option B, "Conditional logic," names a different tag family: {% if %}, {% elif %}, {% else %}. Conditionals select between alternative blocks; they do not repeat a block once per list element. An if-statement placed inside a for loop is still a conditional, not the loop itself, so B fails as the definition of for-loop usage.

Option C, "Variable assign," corresponds to {% set name = value %} (and to assignments inside with blocks). Assignment stores a value under a name; it neither iterates nor repeats anything. A common confusion is that {% for item in items %} "assigns" item each pass — but the assignment is only a side effect of the iteration, and the defining behavior is repetition over the list, which is what option A states.

Option D, "Filter," refers to the pipe syntax applied to expressions, for example {{ interfaces | sort }} or {{ vlan_id | string }}. Filters transform an output value; they are not loop constructs. Filters can be combined with a loop (iterating interfaces | sort), but the filter alone does not generate repeating configuration, so D is out.

The concrete tie-back to this blueprint: objective 3.3 covers advanced Jinja2 templates built with loops, conditionals, output modifiers, and filters. In a device-level or controller-based template rendered against a YANG-derived JSON payload, the for loop is the piece that turns a list of interfaces into repeated interface-configuration blocks; the if statement chooses variants, set stores intermediate values, and pipes reshape values. Each of options B, C and D therefore names one of the sibling constructs that 3.3 lists alongside loops, which is precisely why they are plausible distractors here.

One mechanism this rests on and the stem does not spell out: Jinja2's for tag requires both a body and a matching {% endfor %} delimiter, and iteration is over the Python-style iterable supplied in the template context — lists, tuples, or dict keys — not over raw text. Rendering a template whose context carries items = ['GigabitEthernet1', 'GigabitEthernet2'] executes the enclosed block twice, appending two interface stanzas to the generated configuration; a single-element list yields one stanza, an empty list yields none. That one-pass-per-element execution is the definition captured by option A and absent from the conditional, assignment, and filter options.

Official Reference

https://jinja.palletsprojects.com/en/stable/templates/#for

This link opens the official vendor documentation in a new tab.

Exam Strategy

When a question names one Jinja2 construct among a set of syntax features, map each option to its literal tag or operator before choosing: for → {% for %}, conditional → {% if %}, assignment → {% set %}, filter → the pipe. In 300-435 template questions, ask what output the construct produces — repetition, branching, stored value, or transformed value — and pick the option describing that output. This separates near-miss siblings that all appear inside the same template.

Practice All 300-435 Questions

Access 150 questions with complete answers and detailed explanations.

View Full 300-435 Practice Test →

← Back to 300-435 Study Guide