4

我正在将电子邮件模板从 Mandrill 移动到 Postmark,这需要将 Handlebars 转换为 Mustachio。在把手中,我有这样的事情:

{{#if some_variable}}
<p>This text uses variable: {{some_variable}}
{{/if}}

根据 Mustache 文档,转换后应该如下所示:

{{#some_variable}}
<p>This text uses variable: {{some_variable}}
{{/some_variable}}

问题是 Postmark 的 Mustachio 使用范围(https://github.com/wildbit/mustachio/wiki#scoping),所以在这种情况下,它需要以下 JSON 模型:

{
    "some_variable": {
        "some_variable": "some_variable_value"
    }
}

代替

{
    "some_variable": "some_variable_value"
}

有谁知道如何关闭 Mustachio 的范围,所以它使用预期的示例 JSON 模型?到目前为止,我看到的唯一解决方法(肮脏的一种)是以这种嵌套对象形式传递模板模型,但我已经发现它不适用于所有情况。在此先感谢,任何帮助表示赞赏。

4

1 回答 1

10

好的,找到了该问题的答案。根据文档https://github.com/wildbit/mustachio/wiki#inverted-groups-or-how-to-make-placeholders在这种情况下我应该做的是:

{{#some_variable}}
<p>This text uses variable: {{.}}</p>
{{/some_variable}}

然后发送 JSON 模型,如:

{
    "some_variable": "some_variable_value"
}

将导致

<p>This text uses variable: some_variable_value</p>

所以问题的答案是使用指向标签值的{{.}}运算符。

于 2016-04-27T09:50:46.370 回答