Mandrill API与Handlebars的”each-loop”不起作用。

huangapple go评论87阅读模式
英文:

Mandrill API with Handlebars "each-loop" not working

问题

在使用Mandrill API发送事务性通讯时遇到了问题。我选择了Handlebars作为模板参数。用户名显示正确,但是列表中的数据(帖子标题)为空。请帮忙指出我做错了什么。谢谢!

以下是模板的代码,发送到端点/messages/send.json

func genHTMLTemplate() string {
    return "code generated template<br>" +
        "Hi {{name}}, <br>" + 
        "{{#each posts}}<div>" +
        "TITLE {{title}}, THIS {{this}}<br>" +
        "</div>{{/each}}"
}

在我的mandrillapp.com设置面板中的API日志中显示了参数:

{
    "key": "xxxxxxxxxx",
    "message": {
        // 其他参数...
        "merge_language": "handlebars",
        "global_merge_vars": null,
        "merge_vars": [
            {
                "rcpt": "xxxxxx@gmail.com",
                "vars": [
                    {
                        "name": "posts",
                        "content": [
                            {
                                "title": "title A"
                            },
                            {
                                "title": "title B"
                            }
                        ]
                    },
                    {
                        "name": "name",
                        "content": "John Doe"
                    }
                ]
            }
        ],
        // 其他参数...
    }
}

以下是收到的电子邮件。在"TITLE"之后应该是"title A"和"title B"。

code generated template
Hi John Doe,
TITLE, THIS Array
TITLE, THIS Array
英文:

Met problem when using Mandrill API to send transactional newsletters. I chose Handlebars for the template parameters. The user name was shown correctly, but data in the list (post titles) were empty. Please help indicate if anything I did wrong. Thank you!

The template is as below, sent to the endpoint /messages/send.json :

func genHTMLTemplate() string {
    return &quot;code generated template&lt;br&gt;&quot; +
        &quot;Hi {{name}}, &lt;br&gt;&quot; + 
        &quot;{{#each posts}}&lt;div&gt;&quot; +
	    &quot;TITLE {{title}}, THIS {{this}}&lt;br&gt;&quot; +
	    &quot;&lt;/div&gt;{{/each}}&quot;
}

The API log in my Settings panel in mandrillapp.com shows the parameters:

{
&quot;key&quot;: &quot;xxxxxxxxxx&quot;,
&quot;message&quot;: {
        :
    &quot;merge_language&quot;: &quot;handlebars&quot;,
    &quot;global_merge_vars&quot;: null,
    &quot;merge_vars&quot;: [
        {
            &quot;rcpt&quot;: &quot;xxxxxx@gmail.com&quot;,
            &quot;vars&quot;: [
                {
                    &quot;name&quot;: &quot;posts&quot;,
                    &quot;content&quot;: [
                        {
                            &quot;title&quot;: &quot;title A&quot;
                        },
                        {
                            &quot;title&quot;: &quot;title B&quot;
                        },
                    ]
                },
                {
                    &quot;name&quot;: &quot;name&quot;,
                    &quot;content&quot;: &quot;John Doe&quot;
                }
            ]
        }
    ],
      :
    },
    :
}

And below is the email received. "title A" and "title B" are expected after "TITLE".

code generated template
Hi John Doe,
TITLE, THIS Array
TITLE, THIS Array

答案1

得分: 3

Mandrill决定使用一些可怕的、非常可怕的名称创建自定义的Handlebars助手:

https://mandrill.zendesk.com/hc/en-us/articles/205582537-Using-Handlebars-for-Dynamic-Content#inline-helpers-available-in-mandrill

如果你的对象恰好有名为titleurl的键,那么titleurl肯定会给你带来麻烦。为什么他们没有将这些助手命名为toTitleCaseencodeUrl之类的名称,这超出了我的理解。

至于数组和#each,你可以通过使用{{this.title}}而不是{{title}}来解决这个问题。

英文:

Mandrill decided to create custom handlebars helpers with some horrible, HORRIBLE names:

https://mandrill.zendesk.com/hc/en-us/articles/205582537-Using-Handlebars-for-Dynamic-Content#inline-helpers-available-in-mandrill

title and url will definitely give you grief if your objects happen to have keys named title and urlas well. Why they didn't name their helpers something like toTitleCase and encodeUrl is beyond me.

As far as arrays and #each is concerned, you can work around it by using {{this.title}} instead of {{title}}.

答案2

得分: 1

在使用Mandrill的示例代码进行测试后,我发现关键字"title"不起作用。不知道原因(Mandrill的保留关键字?),但是将其替换为"title1"、"titleX"或其他内容可以正确渲染。

{
    "name": "posts",
    "content": [
        {
           "title": "blah blah"  // "title1"或其他内容可以正常工作
        },
}
英文:

After testing with Mandrill's sample code here I found the key "title" just doesn't work. Dunno the reason (a reserved keyword of Mandrill?) but replace it with "title1", "titleX" or something else it can be rendered correctly.

{
    &quot;name&quot;: &quot;posts&quot;,
    &quot;content&quot;: [
        {
           &quot;title&quot;: &quot;blah blah&quot;  // &quot;title1&quot; or something else works
        },
}

答案3

得分: 1

在使用Handlebars作为合并语言时,'title'是Handlebars中保留的助手名称,用于将文本转换为标题大小写。如果你只使用{{title}},默认情况下它会将空文本视为标题。尝试使用{{title title}},这样应该可以正常工作,或者将键名更改为其他名称(如果你不想将标题转换为标题大小写)。

英文:

while using handlebars as the merge language 'title' is the reserved helpername which is used in handlebars which makes your text in title case. If you do only {{title}} by default it considers as title the empty text. try giving it {{title title}} which should work or changing the key name to something else ( if you dont want your title in title case )

答案4

得分: 0

我知道现在有人试图调试这个问题,虽然有点晚了,但这可能对他们有用。在Mandrill的文档中注意以下几点:

> 通过合并标签添加动态内容有两种主要方式:Handlebars或Mailchimp合并语言。您可能已经熟悉了Mailchimp合并语言,因为您创建和编辑Mailchimp模板时可能已经使用过它。我们还提供了Handlebars的自定义实现,它是开源的,并且提供了更大的灵活性。

> 要设置合并语言,请转到发送默认值,并从合并语言下拉菜单中选择Mailchimp或Handlebars。

我在Sending Blue上遇到了类似的问题,他们的默认配置不启用Handlebars,因此不会对其进行评估。

英文:

I know this is late but it could be of use to someone trying to debug this issue currently. Take note of this point in the Mandrill documentation

> There are two main ways to add dynamic content via merge tags: Handlebars or the Mailchimp merge language. You may already be familiar with the Mailchimp merge language from creating and editing Mailchimp templates. We also offer a custom implementation of Handlebars, which is open source and offers greater flexibility.

>To set your merge language, navigate to Sending Defaults and select Mailchimp or Handlebars from the Merge Language drop-down menu.

I've run into a similar issue on Sending Blue, where their default configuration does not enable handle bars so it won't evaluate them.

huangapple
  • 本文由 发表于 2015年4月12日 17:53:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/29588125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定