Jinja call Block
n
The **call block** in Jinja2 is a special construct that allows you to pass a segment of template code to a macro. This is extremely useful for creating reusable container-style macros that wrap content with a consistent structure, like a dialog box, a panel, or a card component.
nn
How it Works
n
The call block works in tandem with a macro that has access to the caller variable. The block of code between the `{% call … %}` and `{% endcall %}` tags is captured and made available to the macro’s caller variable. When the macro’s code executes `{{ caller() }}`, it renders the content from the `call` block in that exact spot.
n
This is fundamentally different from passing a simple variable to a macro; you are passing an entire section of the template, which can contain its variables, loops, and logic.
nn
nn
Example
n
The following example demonstrates a macro that renders a dialog box, with the content passed using the call block.
nn
Macro Definition
n
{% macro render_dialog(title, class='dialog') -%}n <div class="{{ class }}">n <h2>{{ title }}</h2>n <div class="contents">n {{ caller() }}n </div>n </div>n{%- endmacro %}
n
Call Block Usage
n
{% call render_dialog('Hello World') %}n This is a simple dialog rendered by using a macro andn a call block.n{% endcall %}
n
The final output will be a complete HTML structure with “Hello World” as the title and the custom message as the content inside the div.
nn
n
n
