Gill Bates
Gill Bates

Reputation: 15147

How to override block of included template in Django?

Let's say I have this structure:

{# base.html #}
{% block content %}{% endblock %}

{# page.html #}
{% extends "base.html" %}
{% block content %}
{% include "snippet.html" %}
{# and I also want somehow to redefine {% block snippet_content %} of snippet here #}
{% endblock %}

{# snippet.html #}
<bells_and_whistles>
    {% block snippet_content %}{% endblock %}
</bells_and_whistles>

I hope that the code is self-explaining.

Is there an elegant way to achieve this?

Upvotes: 1

Views: 665

Answers (1)

Spc_555
Spc_555

Reputation: 4121

I'm afraid it is not possible in the way you want to do it.

Your options are:

  1. Create a modified_snippet.html inheriting from snippet.html and overriding the block and include it instead
  2. Change your snippet_content block to a {{ snippet_content }} variable and pass its contents using {% include "snippet.html" with snippet_content="My content" %}. Of course this method is quite limited.

Upvotes: 1

Related Questions