使用可变数量变量的Python中的HTML电子邮件 [英] HTML emailing in Python with variable number of variables

查看:150
本文介绍了使用可变数量变量的Python中的HTML电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定:

  today_ids = ['id1','id2','id5'] 
base_line_ids = ['id1','id2','id3','id4']

added_ids = set(today_ids).difference(base_line_ids)
removed_ids = set(base_line_ids).difference(today_ids )

如果len(remove_ids):
print('以下ids被删除:\\\
{}'format('\\\
'.join(removed_ids)))$
print('没有ids删除')

如果len(added_ids):
print('添加了以下ids:\\\
{}' .format('\\\
'.join(added_ids)))
else:
print('No id添加')

...并添加一些额外的处理,我将最终得到:





  • 添加的id-href对的 zip 的remove_ids列表,可能为也可能不为空。 ,每次可能也可能不是空的。



我现在需要构建一个HTML电子邮件模板,考虑到/或/两个列表可能已填充或为空。以下是一个可能的排列的示例模板:其中一个ID被删除,另外两个添加

  html =\ 
< html>
< head>< / head>
< body>
< h2&更改主页:< / h2>
< h3>指令ID已添加< / h3>
< a href = link_to_url_of_instruction_id_X>指令ID#X< / a>
< a href = link_to_url_of_instruction_id_Y>指令ID#Y / a>
< h3>指令ID被移除< / h3>
< a href = link_to_url_of_instruction_id_A>指令ID#A& ;
< / p>
< / body>

< / html>

我认为这是一个过度的(也许可能)尝试动态创建模板本身,即只添加li如果实际上删除了删除指令ID。在这种情况下(如果没有添加ID ),我会解决插入文本无。



我知道 str.format() string.Template 可以用于在变量数量已知时插入变量值提前。但是,当变量的数量可以变化时,我们如何可以即时插入

解决方案

p>使用 Jinja2 等模板语言,您可以使用以下模板:

 < html> 
< head>< / head>
< body>
< h2>对主页面进行了以下更改:< / h2>
< h3>指示ID已添加< / h3>
{%for_add_instruction_ids%}
< a href ={{instruction_id.url}}>指令ID#{{instruction_id.id}}< / a>
{%endfor%}
< h3>指令ID已移除< / h3>
{%remove_instruction_ids%}中的instruction_id
< a href ={{instruction_id.url}}>指令ID#{{instruction_id.id}}< / a>
{%endfor%}
< / p>
< / body>
< / html>

您甚至可以通过使用if语句来实现动态模板请求:

 < html> 
< head>< / head>
< body>
< h2>对主页面进行了以下更改:< / h2>
{%if len(added_instruction_ids)%}
< h3>指示ID已添加< / h3>
{%endif%}
{%for _ add_instruction_ids%}
< a href ={{instruction_id.url}}>指令ID#{{instruction_id.id} }< / A>
{%endfor%}
{%if len(removed_instruction_ids)%}
< h3>指令ID已移除< / h3>
{%endif%}
{%for remove_instruction_ids%}
< a href ={{instruction_id.url}}>指令ID#{{instruction_id.id} }< / A>
{%endfor%}
< / p>
< / body>
< / html>

注意:上述示例假定以下数据传递给模板引擎:

  {
added_instruction_ids:[
{
id:X,
url:link_to_url_of_instruction_id_X
},{
id:Y,
url:link_to_url_of_instruction_id_Y
}
],
removed_instruction_ids:[
{
id:A ,
url:link_to_url_of_instruction_id_A
}
]
}


Given:

today_ids = ['id1', 'id2', 'id5']
base_line_ids = ['id1','id2','id3','id4']

added_ids = set(today_ids).difference(base_line_ids)
removed_ids = set(base_line_ids).difference(today_ids)

if len(removed_ids):
    print('The following ids were removed:\n{}'.format('\n'.join(removed_ids)))
else:
    print('No ids removed')

if len(added_ids):
    print('The following ids were added:\n{}'.format('\n'.join(added_ids)))
else:
    print('No ids added')

... and adding some additional processing, I will eventually end up with:

  • A list of removed_ids, which may or may not be empty, for any given execution.
  • A zip of added id-href pairs, again which may or may not be empty each time.

I now need to build a HTML email template, taking into account that either/or/both list may be populated or empty. The following is a sample template for one possible permutation: where one ID was removed, and two more were added:

  html = """\
    <html>
      <head></head>
      <body>
        <h2>The following changes have been made to the main page:</h2>
           <h3> Instruction IDs Added</h3>
           <a href=link_to_url_of_instruction_id_X>Instruction ID #X</a>
           <a href=link_to_url_of_instruction_id_Y>Instruction ID #Y</a>
           <h3> Instruction IDs Removed</h3>
           <a href=link_to_url_of_instruction_id_A>Instruction ID #A</a>
        </p>
      </body>

</html>

I think it will be overkill (and perhaps possible) to try dynamically create the template itself on the fly, i.e. only adding the line 'Instruction ID Removed" if there was actually one removed. I'll settle for inserting the text 'None' in this case (and in the case where there were no IDs added).

I know str.format() and string.Template can be used to insert variable values when the number of variables are known in advance. But how can we insert values on the fly when the number of variables can, well, vary?

解决方案

Using a templating language like Jinja2, you could use the following template:

<html>
  <head></head>
  <body>
    <h2>The following changes have been made to the main page:</h2>
      <h3> Instruction IDs Added</h3>
      {% for instruction_id in added_instruction_ids %}
        <a href="{{instruction_id.url}}">Instruction ID #{{instruction_id.id}}</a>
      {% endfor %}
      <h3> Instruction IDs Removed</h3>
      {% for instruction_id in removed_instruction_ids %}
        <a href="{{instruction_id.url}}">Instruction ID #{{instruction_id.id}}</a>
      {% endfor %}
    </p>
  </body>
</html>

You could even achieve your "dynamic template" request by using if statements:

<html>
  <head></head>
  <body>
    <h2>The following changes have been made to the main page:</h2>
      {% if len(added_instruction_ids) %}
      <h3> Instruction IDs Added</h3>
      {% endif %}
      {% for instruction_id in added_instruction_ids %}
        <a href="{{instruction_id.url}}">Instruction ID #{{instruction_id.id}}</a>
      {% endfor %}
      {% if len(removed_instruction_ids) %}
      <h3> Instruction IDs Removed</h3>
      {% endif %}
      {% for instruction_id in removed_instruction_ids %}
        <a href="{{instruction_id.url}}">Instruction ID #{{instruction_id.id}}</a>
      {% endfor %}
    </p>
  </body>
</html>

Note: the above examples assume that the following data is passed to the template engine:

{
  added_instruction_ids: [
    {
      id: X,
      url: link_to_url_of_instruction_id_X
    }, {
      id: Y,
      url: link_to_url_of_instruction_id_Y
    }
  ],
  removed_instruction_ids: [
    {
      id: A,
      url: link_to_url_of_instruction_id_A
    }
  ]
}

这篇关于使用可变数量变量的Python中的HTML电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆