Jinja templates

Introduction to dbt

Mike Metzger

Data Engineer

What is a template?

  • A defined format allowing for substitution of information
  • Often a text file
  • Simplifies access and re-use
Introduction to dbt

What is Jinja?

  • Simple text based templating language
  • Used in many tools beyond just dbt
  • {{ ... }} represents a template substitution
  • dbt has many Jinja functions available for use in projects
  • Allows for more dynamic usage of dbt
  • Can use for loops to simplify code
Introduction to dbt

dbt Jinja functions

  • Some of the many Jinja functions available
    • ref: Access models by name
    • config: Access configuration settings
    • docs: Access documentation information
    • Calculations, Loops, etc
Introduction to dbt

Jinja example

  • Without Jinja
    SELECT
    COALESCE(start_date, '2025-01-01') as start_date,
    COALESCE(update_date, '2025-01-01') as update_date,
    COALESCE(end_date, '2025-01-01') as end_date
    FROM Events
    
  • With Jinja
    SELECT
    {% for column in ['start_date', 'update_date', 'end_date'] %}
    COALESCE({{column}}, '2025-01-01') as {{column}}
    {% endfor %}
    FROM Events
    
Introduction to dbt

Let's practice!

Introduction to dbt

Preparing Video For Download...