2

I've added a custom helper in Handlebars to accomplish an if == "some string" type helper. The helper code is this:

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);
});

The template is 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>

I'm getting this error:

Uncaught Error: if_eq doesn't match each

It seems to be a problem with using the {{else}}, because if I only use the if_eq helper without an else, then it works fine. I'm pretty new to Handlebars, so I'm sure I'm missing something stupid.

Hannes Johansson
  • 1,794
  • 2
  • 15
  • 28
cloudwalker
  • 2,346
  • 1
  • 31
  • 69
  • This would suggest using {{else}} {{#if...}}, because there is no else if. http://stackoverflow.com/questions/10736907/handlebars-js-else-if – Roope Aug 08 '15 at 21:39
  • It's working for me... http://codepen.io/anon/pen/pJYEpQ – VLS Aug 08 '15 at 21:41
  • Well, that's odd. I wonder why it doesn't work for me. Maybe it's related to the version of Handlebars. The app I'm working on is using jquery-handlerbars 1.0.0, so that probably has a very outdated version of handlebars in it. – cloudwalker Aug 08 '15 at 23:53
  • Yeah, looks like that is it. They added the syntax for else if in version 3 and we are super far behind. Doing the nested {{else}} with an {{#if_eq}} inside of it seems to have solved it. – cloudwalker Aug 08 '15 at 23:57

1 Answers1

13

if_eq doesn't match if is thrown when you have {{#if_eq stuff "stuff"}} but you end it by mistake in {{/if}} instead of {{/if_eq}}.

Șerban Ghiță
  • 1,899
  • 20
  • 21