[英]Keeping HTML in Django email templates DRY
我已經創建了看起來像這樣的Django html電子郵件模板:
<p style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.6em; font-weight: normal; margin: 10px 0 10px; padding: 0;">
Hello John
</p>
<p style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.6em; font-weight: normal; margin: 10px 0 10px; padding: 0;">
Welcome to the site
</p>
<p style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.6em; font-weight: normal; margin: 10px 0 10px; padding: 0;">
More text here...
</p>
由於它是電子郵件模板,因此我必須使用內聯樣式,因此它看起來很難看,因此我必須重復一遍。 我正在尋找一種改進方法。
我的想法是創建一個模板標簽,該標簽將返回樣式信息:
@register.filter
def style(tag):
tags = {
'p': "style="font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.6em; font-weight: normal; margin: 10px 0 10px; padding: 0;""
# Will add more options here
}
return tags[tag]
然后在我的模板中,我將使用:
<p|style>
Hello John
</p>
<p|style>
Welcome to the site
</p>
<p|style>
More text here...
</p>
我知道將html放在python代碼中並不理想,但是我想不出更好的方法? 有什么想法或更好的方法來實現這一目標?
考慮定義一個CSS類,並將該類分配給所有需要格式化的元素。
template.css
.MyEmailStyle {
font-family: 'Helvetica Neue', 'Helvetica', Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.6em;
font-weight: normal;
margin: 10px 0 10px;
padding: 0;
}
main.html
<p class="MyEmailStyle">
Hello John
</p>
由於您無法在電子郵件模板中包含外部或內部CSS文件,因此請考慮使用此庫 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.