flask jinja macros variable in url_for

flask, jinja2, python

Solution

This should work:

{% macro icon(site, title="") %}
    <img src="{{ url_for('static', filename='icons/%s.png' % site) }}" alt="{{ title }}" class="img-icon">
{% endmacro %}

Problem

I have a flask app with a jinja macro that looks like that ``` {% macro icon(site, title="") %} <img src="{{ url_for('static', filename="icons/XXX.png") }}" alt="{{ title }}" class="img-icon"> {% endmacro %} ``` What I need to do is have the filename equals to the `site` variable passed to the macro. so instead of the XXX it should have the value of `site`. Is there a way to do that?

Original source