4

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 %}
1
  • Another idea: {% if html %}{% extends base.html %}{% else %}{% extends base.txt %}{% endif %} Commented Jan 25, 2013 at 14:36

2 Answers 2

7

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 %}
Sign up to request clarification or add additional context in comments.

Comments

6

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'}.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.