Default value for a Handlebars` template placeholder

Here is my simple solution

first we create a very basic helper called 'choose'

    Handlebars.registerHelper('choose', function (a, b) {return a ? a : b;});

then we use it in the template :)

<p>
{{choose valueFromData 'default-value-in-template'}}
<p>

or of course we can do

    <p>
    {{choose valueFromData defaultValueFromData}}
    <p>

So in your case:

<form method="{{choose method 'get'}}" action="{{choose action 'action.php'}}" class="menu-edit-form">
...                     
    </form>

Hope it helps someone else since this is from 2014 :)


Handlebars has no "default values".
You should use {{#if}} statement to check property is set.

<script type="x-handlebars-template" id="menu-edit-form-tpl">
    <form method="{{#if method}}{{method}}{{else}}POST{{/if}}" action="{{#if action}}{{action}}{{else}}/{{/if}}" class="menu-edit-form">
...
    </form>
</script>

Or if you want a bit cleaner syntax, use the simple helper:

Handlebars.registerHelper('safeVal', function (value, safeValue) {
    var out = value || safeValue;
    return new Handlebars.SafeString(out);
});

which allows you to write like this:

<script type="x-handlebars-template" id="menu-edit-form-tpl">
    <form method="{{safeVal method 'POST'}}" action="{{safeVal action '/'}}" class="menu-edit-form">
...
    </form>
</script>