Natim
Natim

Reputation: 18092

Django Multiple template inheritance

My case is quite simple:

The content of this two mail are the same because I use EmailAlternative to send both in the same mail.

body.txt:

{% block message %}{% endblock %}

{{ site_name }} team
-----------------
If you need help contact use at {{ support_mail }}

body.html:

<html>
    <head>
        <title>{% block title %}{% endblock %}</title>
    </head>
    <body>
        <p>{% filter linebreaksbr %}{% block message %}{% endblock %}{% endfilter %}</p>
        <p><strong>{{ site_name }} team</strong></p>
        <hr/>
        If you need help contact use at <a href="mailto:{{ support_mail }}">{{ support_mail }}</a>
    </body>
</html>

Of course it is a little bit more complex with translation, css and more than one block.

My wish is to define invitation.txt:

{% block message %}Dear {{ first_name|title }} {{ last_name|upper }},

Your inscription has bee accepted. Welcome!
{% endblock %}

I want to be able to load (body.txt, invitation.txt) and also (body.html, invitation.txt) to get my two html parts.

Edit:

Something like that:

invitation/body.txt:

{% extends body.txt invitation.txt %}

invitation/body.html:

{% extends body.html invitation.txt %}

Upvotes: 4

Views: 7032

Answers (2)

benoitbb
benoitbb

Reputation: 108

You can set a variable in the context and pass it to extends template tag.

In invitation.txt:

{% extends base %}
{% block message %}Dear {{ first_name|title }} {{ last_name|upper }},

Your inscription has been accepted. Welcome!
{% endblock %}

Render invitation.txt with context {'base': 'body.txt'}, then with context {'base': 'body.html'}.

Upvotes: 6

furins
furins

Reputation: 5048

You can use include

e.g.: invitation.txt:

Dear {{ first_name|title }} {{ last_name|upper }},

Your inscription has bee accepted. Welcome!

invitation/body.txt:

{% extends body.txt %}
{% block message %}
{% include "invitation.txt" %}
{% endblock %}

invitation/body.html:

{% extends body.html %}
{% block message %}
{% include "invitation.txt" %}
{% endblock %}

Upvotes: 7

Related Questions