[FIXED] Does jinja support multiple blocks in a macro?


I’m using flask with jinja.

I know that you can define a base page template with multiple placeholder blocks:

        [ standard meta tags, etc go here ]
        {% block css %}{% endblock %}
        [ standard page header goes here ]
        {% block content %}{% endblock %}
        [ standard page footer goes here ]
        {% block javascript %}{% endblock %}

And I know that you can define a macro with a single placeholder:

{% macro dialog() %}
    <div class="dialog">
        [ standard dialog header ]
        {{ caller() }}
{% endmacro %}

{% call dialog() %}
    <div class="log-in">
        Log in or sign up! (etc.)
{% endcall %}

But is it possible to define a macro with multiple placeholder blocks?


No, you can’t. While you can pass several parameters to the macro, only one caller can exist. You can nevertheless pass a parameter back from your macro to the calling context and simulate your desired behavior like this:

{% macro twoblocks()%}
    <div class="content-a">
        {{ caller(True) }}
    <div class="content-b">
        {{ caller(False) }}
{% endmacro %}

{% call(isA) twoblocks() %}
    {% if isA %}
        A content
    {% else %}
        B content
    {% endif %}
{% endcall %}

Answered By – muffel

Answer Checked By – Mary Flores (Easybugfix Volunteer)

Leave a Reply

(*) Required, Your email will not be published