Mustaqim Malim
Mustaqim Malim

Reputation: 39

Replace Jinja2 block inside string

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/

Upvotes: 2

Views: 716

Answers (1)

Mustaqim Malim
Mustaqim Malim

Reputation: 39

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 %}

Upvotes: 1

Related Questions