In Jinja2 templating engine (using Flask), I want to achieve something like that:
{% reusable_block avatar(user) %}
<img src='{{ user.avatar }}' title='{{ user.name }}'/>
{% reusable_block %}
and then in various places:
{% for u in users %}
{% call avatar(u) %}
{% endfor %}
However I can't find such a feature (I made up reusable_blocks
for this question) in Jinja documentation. What I need is basically reusable blocks that can take parameters. Any ideas know how can I do that with Jinja2?
You can use macros.
{% macro input(name, value='', type='text', size=20) -%}
<input type="{{ type }}" name="{{ name }}" value="{{value|e }}" size="{{ size }}">
{%- endmacro %}
<p>{{ input('username') }}</p>
<p>{{ input('password', type='password') }}</p>
More documentation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With