else : Fallback Branch Helper
Provides a fallback branch when conditional expressions are false or when collections are empty.
On this page
Syntax
With {{#if}}:
With {{#each}}:
With {{#with}}:
Parameters
No parameters. The {{else}} helper is used as a separator within block helpers.
Examples
With If Statement
Data:
doc.Params["model"] = new {
hasStock = false
};
Output:
<button class="btn-disabled" disabled>Out of Stock</button>
With Each Loop (Empty Collection)
Data:
doc.Params["model"] = new {
items = new object[] { } // Empty array
};
Output:
<ul>
<li class="empty">No items available</li>
</ul>
With Context Switch (Null Object)
Data:
doc.Params["model"] = new {
user = (object)null
};
Output:
<div class="no-profile">
<p>User profile not available</p>
</div>
Multiple Else If with Final Else
Underlying Implementation
In {{#if}} blocks, {{else}} compiles to:
<choose>
<when data-test="{{condition}}">
<!-- If content -->
</when>
<otherwise>
<!-- Else content -->
</otherwise>
</choose>
In {{#each}} blocks, {{else}} creates a conditional wrapper:
<template data-bind="{{collection}}">
<!-- Each content -->
</template>
<template data-test="count({{collection}}) == 0">
<!-- Else content shown when empty -->
</template>
Notes
- Can be used with
{{#if}},{{#each}}, and{{#with}} - Only one
{{else}}block allowed per helper (use{{else if}}for multiple conditions) - For
{{#if}}, the else block renders when all conditions are false - For
{{#each}}, the else block renders when the collection is empty - For
{{#with}}, the else block renders when the object is null or undefined - Position matters: must come after all
{{else if}}blocks