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.
| Group | What it covers | Examples |
|---|---|---|
| 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 lines | The rule that a block is a one-liner, or every one of its tags owns a line — never a mix. | 18 |
| Indentation | How a block's body sits one level inside its tags, accumulating through nested blocks. | 10 |
| Blank lines | How 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 interior | Breaking the inside of an overflowing {{ … }} or {% set %} construct. | 28 |
| Reanchoring | Multi-line constructs re-flowing to the column the surrounding SQL layout lands them at. | 9 |
| SQL interaction | Where Jinja meets SQL layout — tags straddling clauses, guards on select items, closers on CTEs. | 33 |
| Files of tags | Files that are mostly Jinja — macros and tag scaffolding — with little or no SQL between the tags. | 7 |
| Hygiene | Whitespace 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 |
| Alignment | The align option in templates: rows inside guards and loops join the surrounding column, and what ends a group. | 44 |