2

I want to replace a part of a url_for link with a template block that specifies the filename. I followed something similar to Block tag inside url_for() in Jinja2. But I want to put the image in the base templates main block.

This is the base file:

{% macro error_img(name) -%}
<img class="center-block" viewBox="0 0 60 55" width="300" height="300"
     src="{{ url_for('static', filename=name) }}" alt=""/>
{%- endmacro %}

{% block main %}
<div class="container">
  <div class="row text-center">
    <div class="col"> <!-- I want the image HERE -->
      {{ error_img(name) }}
    </div>1
  </div>
</div>

And in the child template:

{% extends "errors/base.html" %}

{{ error_img("media/errors/404.svg") }}

But I get an image with the url: http://localhost:5000/static/

1 Answer 1

1

For some reason a block is required around the macro:

base.html

{% macro error_img(name) -%}
<img class="center-block" viewBox="0 0 60 55" width="300" height="300"
     src="{{ url_for('static', filename=name) }}" alt=""/>
{%- endmacro %}

{% block main %}
<div class="container">
  <div class="row text-center">
    <div class="col"> <!-- I want the image HERE -->
      {% block img %}
        {{ error_img(name) }}
      {% endblock%}
    </div>
  </div>

child.html

{% extends "errors/base.html" %}

{% block img %}
{{ error_img('media/errors/404.svg') }}
{% endblock %}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.