【问题标题】:How do I include a variable in an "ifequal" handlebar?如何在“ifequal”车把中包含变量?
【发布时间】:2017-03-23 20:42:25
【问题描述】:

我正在使用 Foundation for Sites,它使用 Panini and Handlebars 进行 JS 模板。我需要一个包含 js 变量的 {{#ifequal}} 语句,以便我可以将 URL 参数与 JSON 进行比较

我现在拥有的:

{{#ifequal "Canada" this.destination}} //do the thing {{/ifequal}}

我需要的是这样的:

{{#ifequal <script>document.write(destId)</script> this.destination}} //do the thing {{/ifequal}}

js var "destId" 是在页面前面从 URL 参数中拉出时分配的,但我当然不能在把手中包含脚本。如果有办法将 URL 参数直接传递到也可以工作的车把。

【问题讨论】:

    标签: handlebars.js zurb-foundation


    【解决方案1】:

    如前所述(link is here):

    Handlebars partials 采用第二个参数,该参数成为部分的上下文:

    {{&gt; person this}}

    在 v2.0.0 alpha 及更高版本中,您还可以 传递命名参数的哈希:

    {{&gt; person headline='Headline'}}

    你可以看到这些测试 场景: https://github.com/wycats/handlebars.js/blob/ce74c36118ffed1779889d97e6a2a1028ae61510/spec/qunit_spec.js#L456-L462 https://github.com/wycats/handlebars.js/blob/e290ec24f131f89ddf2c6aeb707a4884d41c3c6d/spec/partials.js#L26-L32

    【讨论】:

    • 太棒了。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多