Skip to main content

Jinja Examples

A gallery of real dbt/Jinja templates before and after dfmt, grouped by construct. Every pair is a passing case from dfmt's own test corpus — the input template on the left, dfmt's output on the right, with every {{ … }} and {% … %} reproduced exactly. 336 examples in all.

These pages are generated from suites/dfmt-jinja/cases/. The corpus also carries cases for shapes dfmt does not yet lay out the way its authors want; those are held back here, so nothing on this page is aspirational — it is what the formatter does today.

GroupWhat it coversExamples
Expressions{{ … }} output expressions embedded in SQL — refs, filters, splits, and the width they declare.24
Branches{% if %} / {% elif %} / {% else %} blocks: where the tags land, and how each branch — taken or not — is laid out.34
Loops{% for … %} loops, including inline generators and loop-controlled separators like {% if not loop.last %}.27
Comments{# … #} Jinja comments, and how they attach to the code above and below them.48
Tag linesThe rule that a block is a one-liner, or every one of its tags owns a line — never a mix.18
IndentationHow a block's body sits one level inside its tags, accumulating through nested blocks.10
Blank linesHow the author's blank lines — the one grouping signal a template has — survive formatting.31
Whitespace control{%- and -%} trim markers, and the glue an absent-whitespace junction implies.7
Construct interiorBreaking the inside of an overflowing {{ … }} or {% set %} construct.28
ReanchoringMulti-line constructs re-flowing to the column the surrounding SQL layout lands them at.9
SQL interactionWhere Jinja meets SQL layout — tags straddling clauses, guards on select items, closers on CTEs.33
Files of tagsFiles that are mostly Jinja — macros and tag scaffolding — with little or no SQL between the tags.7
HygieneWhitespace hygiene: no trailing spaces, no tabs, no invented blank lines, no stray indentation.5
Set blocks{% set x %}…{% endset %} blocks whose body is SQL, laid out where the block lands.11
AlignmentThe align option in templates: rows inside guards and loops join the surrounding column, and what ends a group.44