Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does jinja support multiple blocks in a macro?

Tags:

flask

jinja2

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?

like image 369
Shahaf Avatar asked Sep 30 '22 16:09

Shahaf


1 Answers

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 %}
like image 158
muffel Avatar answered Oct 19 '22 03:10

muffel