Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23390

Jinja2 (flask): How to pass blocks to a macro?

$
0
0

I'm implementing a python flask webapp and I'm trying to write a macro to which I want to pass three blocks of html-code, but couldn't get it run.

I found a simple example on making use of jinja with fastapi here: https://www.slingacademy.com/article/fastapi-how-to-use-macros-in-jinja-templates/?utm_content=cmp-trueI expected that it works, but I couldn't get it run. I tried it and got the error message: "jinja2.exceptions.TemplateAssertionError: block 'content' defined twice".

{% macro panel(title, class='panel') %}<div class="{{ class }}"><h2>{{ title }}</h2>  {% block content %}{% endblock %}</div>{% endmacro %}{% call(panel, title='My Panel') %}{% block content %}<p>This is a panel content.</p>{% endblock %}{% endcall %}

Should this example work or is there a bug in the jinja2 template? Is there an alternative/better way to pass html-code blocks to a macro? I recognized the "caller" approach, but I think it only works with one code block, right?

Thank you!

Kind regards,Jerome

EDITIt looks like the example is to much simplified. I would like to pass 2 or 3 html or even jinja code blocks to the macro. The background is that I want to simplify the use of Bootstrap Accordion which I want to use multiple times in same way.Extending the example it's more like this:

{% macro panel(title, class='panel') %}<div class="{{ class }}"><h2>{{ title }}</h2>  {% block content1 %}{% endblock %}<!-- Do some other stuff in between. -->  {% block content2 %}{% endblock %}</div>{% endmacro %}{% call(panel, title='My Panel') %}{% block content1 %}<p>This is a panel content.</p>{% endblock %}{% block content2 %}<p>This is the detailed panel content.</p>{% endblock %}{% endcall %}

Viewing all articles
Browse latest Browse all 23390

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>