advanced-jinja-patterns

Generate dynamic SQL with reusable Jinja macros in dbt.

1|1|Updated Feb 27, 2026
One-click install
npx skills add https://github.com/nrakow/ae-skills-dev --skill advanced-jinja-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: advanced-jinja-patterns
Source: https://github.com/nrakow/ae-skills-dev/tree/main/skills/advanced-jinja-patterns
Command: npx skills add https://github.com/nrakow/ae-skills-dev --skill advanced-jinja-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Writing robust Jinja for dbt models can be error-prone and hard to maintain. This skill provides clear patterns for dynamic SQL generation, macros, adapter-aware logic, and safe compile-time vs run-time behavior to keep models reliable across warehouses.

Core Features & Use Cases

  • Reusable Jinja macros and patterns for dynamic SQL generation across warehouses
  • Adapter-dispatch aware templates with safe default implementations
  • Guidance on compile-time vs run-time behavior and safeguards like execute guards and safe empty-list handling

Quick Start

Refactor a dbt model to use dynamic loops, namespace mutations, and adapter-aware macros with clear separation between compile-time and run-time.

Frequently Asked Questions about advanced-jinja-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write reusable Jinja macros in dbt for dynamic SQL generation?

Reusable Jinja macros for dbt use dynamic loops and namespace mutations to generate SQL. This approach applies adapter-aware logic and compile-time separation to produce reliable, cross-warehouse SQL models.

What is the difference between compile-time and run-time behavior in dbt Jinja?

Compile-time behavior in dbt Jinja executes during SQL generation, while run-time behavior executes in the warehouse. Safe Jinja patterns use execute guards to prevent errors when database objects are unavailable during compilation.

How do I create adapter-aware macros that work across multiple warehouses in dbt?

Adapter-aware macros in dbt use adapter dispatch patterns with safe default implementations. This dispatch mechanism routes Jinja logic to warehouse-specific functions while maintaining a consistent fallback.

Does dbt Jinja support safe handling for empty lists in dynamic SQL?

dbt Jinja supports safe empty-list handling by applying guard clauses before loop execution. This prevents SQL syntax errors when macros iterate over filters or parameters that contain no elements.

What is the best way to refactor dbt models to use dynamic Jinja patterns?

The best way to refactor dbt models with dynamic Jinja is separating compile-time logic from run-time execution. This involves replacing inline code with adapter-dispatch macros, dynamic loops, and namespace mutations.

Why does my dbt Jinja macro fail during compilation when the database connection is off?

dbt Jinja macros fail during compilation without execute guards because they attempt run-time database queries. Adding execute guards ensures macros skip database interactions when compiling SQL offline.