...

/

Setting Up Jinja Variables

Setting Up Jinja Variables

Learn how to use the set instruction and the if statement in Jinja.

Jinja variables

In addition to project and environment variables, dbt allows us to define variables at the file level. This is done using the set instruction.

Press + to interact
{% set first_name="Peter" %}

The variable can then be rendered using double curly brackets:

Press + to interact
main.py
jinja.sql
from jinja2 import Environment, FileSystemLoader
environment = Environment(loader=FileSystemLoader(""))
template = environment.get_template("jinja.sql")
print(template.render())

The default filter

If we’re not sure that the variable is defined, we can pass a default value using the default filter.

Press + to interact
main.py
jinja.sql
from jinja2 import Environment, FileSystemLoader
environment = Environment(loader=FileSystemLoader(""))
template = environment.get_template("jinja.sql")
print(template.render())

If the variable name is not defined, this will render as:

Press + to interact
SELECT "John Doe" AS name

Variable types

Jinja is written in Python, therefore, ...