Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Multiple template inheritance

My case is quite simple:

  • I have a template for text/plain mails : body.txt
  • Another one for text/html mails : body.html

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 %}
like image 658
Natim Avatar asked Dec 12 '22 18:12

Natim


1 Answers

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 %}
like image 73
furins Avatar answered Dec 21 '22 01:12

furins