Jinja Syntax

Jinja is a templating engine for Python. It’s most frequently used in Django and Flask applications. In Coalesce, you can use it to expand SQL capabilities.

These are just some of the syntax options available with Jinja. See Template Designer Documentation for more.

Variables

Variables in Jinja are defined by double curly braces. Jinja replaces the {{ variable_name }} with the value.

{{ variable_name }}

Statement

Statements control logic and flow. For example, starting a loop.

{% %}

//For Loop Example
{% set cookies = ['chocolate chip', 'oatmeal', 'sugar'] %}

{%- for cookies in cookies -%}

I love {{cookies}} cookies!

{% endfor %}

Comments

Comments are not evaluated when the code is run. Comments are a good place to put information such as what the code does and the expected output.

{# Some comment info here
More comment information
#}

Whitespace

Jinja will print any whitespace in the code block. To prevent that, adding a dash to the left or right of the percent sign will remove the whitespace.

{%- -%}

Filters
Filters are designed by a pipe, |. They change the look, format, or even output new data.

{{ name | capitalize }}

Set

Set a variable value.

{% set company_name = "Coalesce" %}

For Loop

A for loop will loop over a sequence until a condition is met. For example, you have a list of cookies, chocolate chips, oatmeal, and sugar. For every cookie in the list, print out “I love cookies”. “I love cookies” will be printed three times.

{% set cookies = ['chocolate chip', 'oatmeal', 'sugar'] %}
{%- for cookies in cookies -%}
    I love {{cookies}} cookies!
{% endfor %}


I love chocolate chip cookies!

If/Else

Use if/else to provide instructions such as if this condition is met, then to this action.

{% set color = 'orange' %}
{% if color == 'orange' %}

I love orange!

{% else %}

I only want orange

{% endif %}

List

Lists are formatted using brackets, []. Lists are indexed starting at 0. For example, chocolate chip has an index of 0.

{% set cookies = ['chocolate chip', 'oatmeal', 'sugar'] %}

Dictionary

Dictionaries use key:value pairs.

{% set company = {
    'name': 'Coalesce',
    'type': 'Data Transformation',
    'trial': 30
} %}

{{ company.trial }}

Comparison Operators

Jinja inherits the comparison operators from Python. Comparison operators are used to compare values and determine if they equate to true or false.

Available comparison operators

Example:

In this example, a single equal, =, is used to set a variable. Use a single equals to set variables, dictionaries, and lists. A double equal, ==, is used to compare if the color is orange. In the example below, it is orange, it evaluates to true.

{% set color = 'orange' %}
{% if color == 'orange' %}

I love orange!

{% else %}

I only want orange

{% endif %}

Jinja Syntax Summary

Because Jinja inherited from Python, you have many syntax options available. There are a few common ones in this guide. You can also use SQL statements such as CASE WHEN. This makes Jinja a very powerful tool in Coalesce.