Show or hide content based on your data. Master {{#if}}, {{else}}, and {{#unless}} to build dynamic pages that adapt to different states.
Show content only when a variable is truthy:
<script data>
({
showBanner: true,
announcement: "Version 1.2 is out! ๐"
})
</script>
{{#if showBanner}}
<div class="banner">
{{ announcement }}
</div>
{{/if}}
Since showBanner is true, the banner is rendered. Set it to false and the banner disappears entirely from the HTML output.
Add an {{else}} branch for when the condition is falsy:
<script data>
({
loggedIn: false,
username: "Dan"
})
</script>
{{#if loggedIn}}
<p>Welcome back, {{ username }}!</p>
<button>Logout</button>
{{else}}
<p>Please sign in to continue.</p>
<button>Login</button>
{{/if}}
Since loggedIn is false, the else branch renders:
Please sign in to continue.
{{#unless}} is the opposite of {{#if}} โ it renders when the variable is falsy.
<script data>
({
hideFooter: false
})
</script>
{{#unless hideFooter}}
<footer>
<p>This footer appears because hideFooter is false.</p>
</footer>
{{/unless}}
When to use {{#unless}}: It's cleaner than {{#if}} + negation. Use it when you want "show this unless X is true".
<!-- Confusing double-negative -->
{{#if showFooter}}
<footer>...</footer>
{{/if}}
<!-- Clear intent -->
{{#unless hideFooter}}
<footer>...</footer>
{{/unless}}
OrbLayout follows JavaScript's truthy/falsy rules:
| Value | Result | {{#if}} renders? |
|---|---|---|
true | Truthy | โ Yes |
"hello" | Truthy | โ Yes |
42 | Truthy | โ Yes |
"true" | Truthy | โ Yes |
false | Falsy | โ No |
"" (empty string) | Falsy | โ No |
0 | Falsy | โ No |
null | Falsy | โ No |
undefined | Falsy | โ No |
Note: If a variable doesn't exist in your data block, it's undefined (falsy). So {{#if missingVar}} won't render.
{{#if showHero}}
<use component="hero" heading="Welcome!" />
{{/if}}
<script data>
({
products: [
{ name: "Widget", price: 9.99, onSale: true },
{ name: "Gadget", price: 24.99, onSale: false },
{ name: "Doohickey", price: 4.99, onSale: true }
]
})
</script>
{{#each products}}
<div class="product">
<h3>{{ name }}</h3>
<p>${{ price }}</p>
{{#if onSale}}
<span class="sale-badge">๐ฅ SALE</span>
{{/if}}
</div>
{{/each}}
{{#each items}}
{{#if @first}}
<div class="item first-item">{{ this }}</div>
{{else}}
<div class="item">{{ this }}</div>
{{/if}}
{{/each}}
You should now be able to:
{{#if variable}}{{else}}{{#unless variable}}