Mustachio:没有作用域的 if 语句 - 这可能吗?

Mustachio: if-statement without scoping - is it possible?

我正在将电子邮件模板从 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 模型?到目前为止,我看到的唯一解决方法(脏方法)是以这种嵌套对象形式传递模板模型,但我已经发现它在所有情况下都不起作用。在此先感谢您的帮助。

好的,找到了该问题的答案。根据文档 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>

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