Django Multiple Template Inheritance

My case is pretty simple:

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

The content of this mail is the same because I use EmailAlternative to send 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, this is a bit more complicated with translation, css and more than one block.

My desire is to define a .txt prompt :

 {% block message %}Dear {{ first_name|title }} {{ last_name|upper }}, Your inscription has bee accepted. Welcome! {% endblock %} 

I want to be able to upload (body.txt, .txt prompt) as well as (body.html, .txt prompt) 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 %} 
+4
source share
2 answers

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

At the .txt prompt:

 {% extends base %} {% block message %}Dear {{ first_name|title }} {{ last_name|upper }}, Your inscription has been accepted. Welcome! {% endblock %} 

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

+5
source

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

All Articles