2013-01-25 154 views
4

我的情況很簡單:Django的多模板繼承

  • 我有text/plain的郵件模板:body.txt
  • 另外一個text/html類型的郵件:body.html

這兩封郵件的內容相同,因爲我使用EmailAlternative在同一封郵件中發送。

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> 

當然是有翻譯,CSS和一個以上的塊多一點點複雜。

我的願望是定義invitation.txt

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

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

我希望能夠加載(body.txt,invitation.txt)以及(body.html,invitation.txt)至得到我的兩個html部分。

編輯:

類似的東西:

邀請/ body.txt

{% extends body.txt invitation.txt %} 

邀請/ body.html

{% extends body.html invitation.txt %} 
+0

另一個想法:''{%if html%} {%extends base.html%} {%else%} {%extends base.txt%} {%endif%}' – Natim

回答

5

您可以在上下文中設置一個變量並將其傳遞給extends模板標籤。

在invitation.txt:

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

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

渲染invitation.txt具有上下文{'base': 'body.txt'},然後用上下文{'base': 'body.html'}

7

你可以使用include

例如爲: invitation.txt

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

Your inscription has bee accepted. Welcome! 

邀請/ body.txt

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

邀請/ body.html

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