Mandrill模板未在数组上循环

2024-04-23 22:39:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图将项目数组发送到Mandrill模板,但它不起作用,我收到的邮件显示的是循环本身,而不是数据。你知道吗

下面是我的python代码:

message = {
    'text': '',
    'html': '',
    'preserve_recipients': False,
    'to': [
        {
            'email': 'rohit@email.com',
            'name': 'Rohit Khatri',
            'type': 'to'
        }
    ],
    'merge_vars': [
        {
            'rcpt': 'rohit@email.com',
            'vars': [
                {
                    'name': 'name',
                    'content': 'Rohit Khatri'
                },
                {
                    'name': 'email',
                    'content': 'rohit@email.com'
                }
            ]
        }
    ],
    'global_merge_vars': [
        {
            'name': 'notifications',
            'content': [
                {
                    'name': 'Rohit Khatri',
                    'profile_picture': 'http://rohitkhatri.com/profile_picture.jpg'
                },
                {
                    'name': 'John Doe',
                    'profile_picture': 'http://johndoe.com/profile_picture.jpg'
                }
            ]
        }
    ]
}

api = Mandrill('MANDRILL_KEY')
api.messages.send_template(
    template_name = 'notification',
    template_content = {},
    message = message
)

模板代码如下:

{{#each notifications}}
<tr class="item">
    <td valign="top" class="textContent">
        <img src="{{profile_picture}}" width="50" height="75" class="itemImage"/>
        <h4 class="itemName">{{name}}</h4>
    </td>
</tr>
{{/each}}

这是我在电子邮件中得到的结果:

{{#notifications}}
{{name}}
{{/notifications}}

它没有执行循环,不知道我做错了什么。你知道吗


Tags: namecommessageemailtemplatevarscontentprofile