Skip to main content

Variables

Variables are the simplest and most common Jinja2 feature. They let you insert dynamic values from your JSON data into the template.

Basic syntax

Wrap the variable name in double curly braces:

Hello {{ name }}

With this JSON:

{
"name": "Mike"
}

The output is:

Hello Mike

Accessing nested data

If your JSON has nested objects, use dot notation:

{
"customer": {
"name": "Jane",
"email": "jane@example.com"
}
}
<p>Name: {{ customer.name }}</p>
<p>Email: {{ customer.email }}</p>

Output:

Name: Jane
Email: jane@example.com

Accessing array items

Use bracket notation or dot notation with the index:

{
"colors": ["red", "green", "blue"]
}
<p>First color: {{ colors[0] }}</p>
<p>Second color: {{ colors[1] }}</p>

Default values

If a variable might not exist, you can provide a fallback:

{{ company_name | default("N/A") }}

If company_name is missing from the JSON, it will display "N/A" instead of an empty string.

Variables in different contexts

You can use variables anywhere in your HTML — inside text, attributes, styles, and more:

<h1 style="color: {{brand_color}}">{{title}}</h1>
<img src="{{logo_url}}" alt="{{company_name}} logo" />
<a href="{{website}}">Visit our website</a>

Next steps

  • Conditions — show or hide content based on values
  • Loops — repeat content for lists