python中使用mako模版Template

最近在开发中用到了发送邮件的功能,邮件内容要求是html,所以就用到了mako的模版功能。

mako模版可以让我们在后端进行html的数据填充,从而获取html文件

使用方法:

1、pip安装Mako

  pip install Mako

2、设置发送邮件的模版

<div style="margin:0px 0 10px 0px">
        <p>您好!您所申请的虚拟机详情</p>
        <P>${title}</P>
        <div style="margin-top:0px">
            <P>详情:</P>
            <table style="border: 1px solid #D2D2D2; font-size: 14px;">
              <tr>
                <th style="padding: 10px; text-align: center; border-right: 1px solid #D2D2D2;">IP</th>
                <th style="padding: 10px; text-align: center; border-right: 1px solid #D2D2D2;">端口</th>
                <th style="padding: 10px; text-align: center; border-right: 1px solid #D2D2D2;">用户名</th>
                <th style="padding: 10px; text-align: center; border-right: 1px solid #D2D2D2;">密码</th>
                <th style="padding: 10px; text-align: center; border-right: 1px solid #D2D2D2;">创建状态</th>
                <th style="padding: 10px; text-align: center; border-right: 1px solid #D2D2D2;">Agent安装状态</th>
                <th style="padding: 10px; text-align: center;">业务模块迁移状态</th>
              </tr>
                %for data in datas:
                  <tr>
                    <td style="padding: 10px; text-align: center; border-top: 1px solid #D2D2D2; border-right: 1px solid #D2D2D2;">${data['ip']}</td>
                    <td style="padding: 10px; text-align: center; border-top: 1px solid #D2D2D2; border-right: 1px solid #D2D2D2;">${data['port']}</td>
                    <td style="padding: 10px; text-align: center; border-top: 1px solid #D2D2D2; border-right: 1px solid #D2D2D2;">${data['account']}</td>
                    <td style="padding: 10px; text-align: center; border-top: 1px solid #D2D2D2; border-right: 1px solid #D2D2D2;">${data['password']}</td>
                      %if data['create_status'] == 'FAILED':
                        <td style="padding: 10px; text-align: center; border-top: 1px solid #D2D2D2; border-right: 1px solid #D2D2D2; color: #FF5656;">${data['create_status']}</td>
                      %else:
                        <td style="padding: 10px; text-align: center; border-top: 1px solid #D2D2D2; border-right: 1px solid #D2D2D2; color: #34D97B;">${data['create_status']}</td>
                      %endif

                      %if data['agent_status'] == 'FAILED':
                        <td style="padding: 10px; text-align: center; border-top: 1px solid #D2D2D2; border-right: 1px solid #D2D2D2; color: #FF5656;">${data['agent_status']}</td>
                      %else:
                        <td style="padding: 10px; text-align: center; border-top: 1px solid #D2D2D2; border-right: 1px solid #D2D2D2; color: #34D97B;">${data['agent_status']}</td>
                      %endif

                      %if data['transfer_status'] == 'FAILED':
                        <td style="padding: 10px; text-align: center; border-top: 1px solid #D2D2D2; border-right: 1px solid #D2D2D2; color: #FF5656;">${data['transfer_status']}</td>
                      %else:
                        <td style="padding: 10px; text-align: center; border-top: 1px solid #D2D2D2; color: #34D97B;">${data['transfer_status']}</td>
                      %endif
                  </tr>
                %endfor
            </table>
        </div>
    </div>

3、使用Template

from mako.template import Template
email_content = Template(common.EMAIL_TEMPLATE).render(title=vm_detail, datas=content)
print email_content

email_content就是我们最终需要的html代码。

常用语法

变量

${name} 

在{}中可以执行Python语句,比如${name.upper()}会转为大写

循环

% for i in l:
    ${i}
% endfor

条件

% if i == 1:
    ${i}
%else:
    ${i+1}
% endif
原文地址:https://www.cnblogs.com/wangyingblock/p/12973629.html