【发布时间】:2016-11-04 15:34:36
【问题描述】:
我创建了 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代码中并不理想,但我想不出更好的方法? 有什么想法或更好的方法来实现这一目标?
【问题讨论】: