Setting Up Jinja Variables
Learn how to use the set instruction and the if statement in Jinja.
We'll cover the following...
We'll cover the following...
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.
{% set first_name="Peter" %}
The variable can then be rendered using double curly brackets:
Python
Files
from jinja2 import Environment, FileSystemLoaderenvironment = 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.
Python
Files
from jinja2 import Environment, FileSystemLoaderenvironment = Environment(loader=FileSystemLoader(""))template = environment.get_template("jinja.sql")print(template.render())
If the variable name is not defined, this will render as:
SELECT "John Doe" AS name
Variable types
Jinja is written in Python, therefore, the variables can have any of the native Python data types, such as: ...