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!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.