Introduction to Jinja
Discover the fundamentals of Jinja, a templating language.
What is Jinja?
One of the greatest powers of dbt is that it uses a “template engine” called Jinja. A template engine is a tool that allows developers to create templates and pass data to these templates to generate dynamic content.
Template engines are typically used in web development. E-commerce websites use template engines to display all their products dynamically. In a template, they would write something similar to this:
{% for product in products %}<div><img src={{ product.img_link }}><p> {{ product.name }} </p></div>{% endfor %}
This template would then receive a list of products and dynamically generate the HTML for that web page. Thanks to template engines, developers don’t have to change the HTML code every time there’s a change in the product list.
But templating does not only apply to HTML. It’s useful for every type of file, including SQL files. Jinja enables us to add dynamic logic into our SQL logic, such as:
Control structures (
if
statements andfor
loops)Environment ...