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

Issue

I’m using flask with jinja.

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

<html>
    <head>
        [ standard meta tags, etc go here ]
        {% block css %}{% endblock %}
    </head>
    <body>
        [ standard page header goes here ]
        {% block content %}{% endblock %}
        [ standard page footer goes here ]
        {% block javascript %}{% endblock %}
    </body>
</html>

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

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

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

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

Solution

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>
    <div class="content-b">
        {{ caller(False) }}
    </div>
{% 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