Jinja2 `is defined` test guards undefined variables

300-435 By islever Editorial Last updated

Short answer

A The Jinja2 `is defined` test checks whether a variable exists in the template context before it is used, preventing undefined-variable errors.

Question

What is Jinja2 'is defined' test?
  1. Checks if variable exists before using Correct Answer
  2. Defines variable
  3. Tests type
  4. Tests length

Key Takeaways

The Jinja2 is defined test checks whether a variable exists in the rendering context before the template dereferences it, preventing UndefinedError. Option A is correct because the test returns a boolean existence check rather than assigning, type-checking or measuring length. Guarding with {% if variable is defined %} lets templates render safely when optional fields such as SNMP community strings are absent.

Expert Analysis

{% if variable is defined %} prevents errors from undefined variables in templates.

The deciding mechanism here is Jinja2's undefined-variable semantics: when a template references a name that was never passed into the rendering context, Jinja2 raises UndefinedError at render time — or, depending on configuration, silently renders an empty string. The is defined test is the guard that lets a template interrogate the context before that reference is ever evaluated, which is exactly what option A, "Checks if variable exists before using," describes.

Why each option holds or fails

  • A (correct): {% if snmp_community is defined %} evaluates to true only when snmp_community exists in the current template context. This is the standard defensive idiom for optional variables — a Jinja2 filter or loop body that would otherwise dereference a missing key is placed inside the guard, so a partially populated data model renders safely instead of aborting the whole template.
  • B, "Defines variable": assignment in Jinja2 is a different construct entirely — {% set snmp_community = 'public' %} or a default filter creates or overwrites a binding. The is defined test is read-only; it inspects the context and returns a boolean, it never writes anything back into it.
  • C, "Tests type": type checking is the job of the separate is number, is string, is mapping, is iterable tests. is defined answers only whether a binding exists, not what kind of value it holds — a variable can be defined and still be None or a string.
  • D, "Tests length": length is checked with the length filter ({{ interfaces | length }}) or the is string-style sequence tests; is defined carries no notion of size.

The mechanism this scenario relies on

What the question never spells out is when the error occurs. Jinja2 resolves names lazily: an Undefined object is created the moment the name is looked up, and it only raises UndefinedError when you actually perform an operation on it — iterating, calling, or indexing it. So {% for line in config_lines %} where config_lines was never supplied fails inside the loop, while merely referencing the name in a test does not. That is precisely why the is defined test can be used as a precondition: it touches the name without triggering the operation that would raise. Working this through with the option set, option A's "before using" is the operative phrase — the guard must precede the dereference, which is why the test appears in an {% if %} header rather than inline in the output. Options B, C and D each describe a different family of Jinja2 constructs (assignment, type tests, length measurement) and none of them matches the boolean existence check that is defined performs.

Official Reference

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

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

Exam Strategy

On Jinja2 questions, separate the four construct families before reading the answers: assignment ({% set %}), existence (is defined), type (is number, is mapping), and size (| length). The word 'before using' in an option is a strong signal for an existence guard, since a guard must run ahead of the dereference that would raise. Apply the same elimination to any template-language question where several options describe valid but different syntax.

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