Skip to main content

Whitespace control

{%- and -%} trim markers, and the glue an absent-whitespace junction implies.

Each pair below is a real, passing case from dfmt's test corpus (suites/dfmt-jinja/cases/whitespace-control/): the template as written, then the same template after dfmt — every Jinja tag reproduced exactly, the SQL laid out around it. 7 examples.

A blank between two trimmed tags ships once​

Left unchanged — already in dfmt's shape:

{%- set a = 1 -%}

{%- set b = 2 -%}

A glued block around a whole statement opens​

dialect snowflake

Before

create table orders (id number);
{% if flag %}grant select on table orders to role reporter;{% endif %}

After

create table orders(id number);

{% if flag %}
grant select on table orders to role reporter;
{% endif %}

A glued block is left exactly as written​

dialect postgres

Left unchanged — already in dfmt's shape:

select a{% if true %}, b{% endif %} from t

A glued block that is the whole file opens​

dialect snowflake

Before

{% if flag %}grant select on table orders to role reporter;{% endif %}

After

{% if flag %}
grant select on table orders to role reporter;
{% endif %}

A trim marked tag re breaks and keeps its follower​

Before

{{ config(materialized = 'incremental',
unique_key = 'careteam_reference_patient_unresolved_metric_identifier',
tags = ['metric', 'referential_integrity', 'careteam']) -}}

-- depends_on: {{ ref('patients') }}

select 1 as x

After

{{
config(
materialized = 'incremental',
unique_key = 'careteam_reference_patient_unresolved_metric_identifier',
tags = ['metric', 'referential_integrity', 'careteam']
)
-}}

-- depends_on: {{ ref('patients') }}

select 1 as x

An untaken glued guard around a statement opens​

dialect snowflake

Before

create table orders (id number);
{% if flag %}grant select on table orders to role reporter;{% endif %}

After

create table orders(id number);
{% if flag %}
grant select on table orders to role reporter;
{% endif %}

Trim markers are preserved​

Before

select
{%- if flag %}
a,
{%- endif %}
b
from t

After

select
{%- if flag %}
a,
{%- endif %}
b
from t