【问题标题】:Mustachio: if-statement without scoping - is it possible?Mustachio:没有范围的 if 语句 - 有可能吗?
【发布时间】:2016-08-21 09:53:10
【问题描述】:

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

{{#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 模型?到目前为止,我看到的唯一解决方法(肮脏的一种)是以这种嵌套对象形式传递模板模型,但我已经发现它不适用于所有情况。提前感谢,任何帮助表示赞赏。

【问题讨论】:

    标签: mustache postmark


    【解决方案1】:

    好的,找到了解决该问题的方法。根据文档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>
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-06
      • 1970-01-01
      • 1970-01-01
      • 2010-12-28
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 2017-04-28
      相关资源
      最近更新 更多