This question and its accepted answer are quite old and a lot of new features and functionality have been added to handlebars since they were written.
I managed to get the functionality of default values by using partial blocks which were released in v4.0.0 - so your template would end up looking like this:
<script type="x-handlebars-template" id="menu-edit-form-tpl">
<form method="{{#> method}}get{{/method}}" action="{{#> action}}index.php{{/action}}" class="menu-edit-form">
...
</form>
</script>
Then it's just a case of passing in your method
and action
as partials by doing something like this:
var source = $('#menu-edit-form-tpl').html(),
template = Handlebars.compile(source),
html = template({}, {
partials: {
action: 'contact-form.php'
}
});
In the resulting html the method will default to get
and the action will be contact-form.php
. Here's a working demo:
var source = $('#menu-edit-form-tpl').html(),
template = Handlebars.compile(source),
html = template({}, {
partials: {
action: 'contact-form.php'
}
});
// Code below here only to show output.
document.write('<code>' + $("<div/>").text(html).html() + '</code>');
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.5/handlebars.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="x-handlebars-template" id="menu-edit-form-tpl">
<form method="{{#> method}}get{{/method}}" action="{{#> action}}index.php{{/action}}" class="menu-edit-form">
...
</form>
</script>