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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM