Learning Jinja2: Part 2: Variable Interpolation

Learning Jinja2

Jinja2 is a template engine for Python featuring an optional sandbox environment.

Variable Interpolation

As a template engine, one of the most common tasks is variable interpolation. In programming terminology, interpolation is the act of substituting the value of a variable for its name.


In templating syntax, a delimiter is a single character or token marking some type of content as instructions or code and not part of output. Jinja2 supports four types.

  • {% ... %}: Statements
  • {{ ... }}: Expressions
  • {# ... #}: Comments
  • #  ... ##: Line Statements

Working with Variables

As a form of expressions, variable values can be interpolated using the double curly bracket usage.

After being loaded with the Template() function, the results can be interpolated using the render() function.

Setting Values

Jinja2 supports the set keyword for changing or initializing values.

Play with the example on Repl.it!