Skip to content

Auto-import project macros from macros/ directory #84

@jlowin

Description

@jlowin

Add support for auto-importing Jinja2 macros from a macros/ directory (similar to dbt's approach).

Proposed behavior

Any .md file in macros/ directory gets loaded and its macros are available globally in all templates without explicit import.

project/
  macros/
    auto_tags.md       # {% macro auto_tags() %}...{% endmacro %}
    auto_title.md      # {% macro auto_title() %}...{% endmacro %}
  models/
    document.md        # Can use {{ auto_tags() }} directly

Implementation

In bind_context_to_environment(), discover and load macros from macros/*.md files.

Benefits

  • Matches dbt convention (familiar pattern)
  • Zero boilerplate for reusable patterns
  • Project-scoped (not global)
  • Enables macro libraries for common defer block patterns

Related

Follow-on from defer blocks work (#68). Common patterns like auto-tagging, TOC generation, and metadata extraction are perfect candidates for reusable macros.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions