我创建了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代码中是不理想的,但我想不出更好的方法? 有任何想法或更好的方法来实现这一目标吗?
答案 0 :(得分:1)
考虑定义一个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文件,请考虑使用此library。