【问题标题】:Handlebars custom if equals helper not compilingHandlebars custom if equals helper 未编译
【发布时间】:2015-08-08 21:14:40
【问题描述】:

我在 Handlebars 中添加了一个自定义助手来完成 if == "some string" 类型的助手。辅助代码是这样的:

Handlebars.registerHelper('if_eq', function(a, b, opts) {
    if(a == b) // Or === depending on your needs
        return opts.fn(this);
    else
        return opts.inverse(this);
});

模板是这样的:

<div id="appStoreParametersModal" class="modal-dialog">
    <div class="modal-content appStoreParametersModal">
        <div class="modal-header hypersignModalHeader">
            <h3>{{appName}}</h3>
        </div>
        <div class="modal-body">
            <div id="app-params">
                {{#each appParm}}
                <form>
                    {{#if_eq uiControlType "text"}}
                    <div class="form-group">
                        <label for="{{qsName}}">{{uiName}}</label>
                        <input type="text" class="form-control" id="{{qsName}}" placeholder="{{uiName}}"/>
                    </div>
                    {{else if_eq uiControlType "dropdown"}}
                    <div class="form-group">
                        <label for="{{qsName}}">{{uiName}}</label>
                        <select class="form-control" id="{{qsName}}">
                            {{#each defaultVals}}
                                <option value="{{value}}">{{displayName}}</option>
                            {{/each}}
                        </select>
                    </div>
                    {{/if_eq}}
                </form>
                {{/each}}
            </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-warning cancel" data-dismiss="modal" id="cancel">Cancel</button>
            <button type="button" class="btn btn-success" id="appStoreNext">Next</button>
        </div>
    </div>
</div>

我收到此错误:

未捕获的错误:if_eq 不匹配每个

使用{{else}} 似乎有问题,因为如果我只使用if_eq 助手而不使用else,那么它工作正常。我对 Handlebars 很陌生,所以我确定我错过了一些愚蠢的东西。

【问题讨论】:

  • 这建议使用 {{else}} {{#if...}},因为没有 else if。 stackoverflow.com/questions/10736907/handlebars-js-else-if
  • 它对我有用...codepen.io/anon/pen/pJYEpQ
  • 嗯,这很奇怪。我想知道为什么它对我不起作用。可能和 Handlebars 的版本有关。我正在开发的应用程序使用的是 jquery-handlerbars 1.0.0,因此其中可能有一个非常过时的把手版本。
  • 是的,看起来就是这样。他们在版本 3 中添加了 else if 的语法,我们远远落后。用 {{#if_eq}} 做嵌套的 {{else}} 似乎已经解决了。

标签: javascript handlebars.js


【解决方案1】:

if_eq doesn't match if 在你有 {{#if_eq stuff "stuff"}} 时被抛出,但你错误地以 {{/if}} 而不是 {{/if_eq}} 结束它。

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    • 1970-01-01
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    相关资源
    最近更新 更多