Skip to content

Add Jinja2-style macros#334

Open
navrocky wants to merge 2 commits into
pantor:mainfrom
navrocky:feature/macros
Open

Add Jinja2-style macros#334
navrocky wants to merge 2 commits into
pantor:mainfrom
navrocky:feature/macros

Conversation

@navrocky

@navrocky navrocky commented May 22, 2026

Copy link
Copy Markdown
Contributor

This PR adds support for Jinja2-style macros — reusable, parameterised template fragments — bringing inja closer to feature-parity with Jinja2 for common templating use cases.

{% macro input(name, value="", type="text") %}
  <input type="{{ type }}" name="{{ name }}" value="{{ value }}"/>
{% endmacro %}

{{ input("login") }}
{{ input("age", "18", "number") }}

Features

  • Definition / call: {% macro name(args) %}...{% endmacro %} and {{ name(args) }}.
  • Default parameter values: {% macro greet(name, greeting="Hello") %}...{% endmacro %}.
  • Isolated scope: a macro body sees only its own parameters and the global input data — set variables and loop variables from the caller are not visible (matches Jinja2 semantics). Parameters never leak back
    to the caller.
  • Macros calling macros, including recursion ({% macro foo(n) %}{% if n > 0 %}{{ foo(n - 1) }}{% endif %}{% endmacro %}).
  • Hoisting from {% include %}: macros defined in an included template become callable in the including template after the {% include %} line. This works both for file-system includes and templates registered
    via env.include_template(...).
  • Error handling: missing required argument → inja::RenderError; duplicate macro name / unmatched {% macro %} → inja::ParserError.

Closes #238

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macros (as in Jinja)

1 participant