Sub topic 9
Rules for writing a valid chain
This chapter assumes you can write a basic LWC with an HTML template and a JavaScript class, bind properties with curly braces, and write getters. You have already seen lwc:if, lwc:elseif, and lwc:else on their own. Here you will learn the rules that make a chain of them compile and behave correctly.
What a chain is
A chain is a group of sibling elements that the compiler treats as one if / else-if / else block. At runtime, at most one branch of the chain renders, and the first branch whose condition is truthy wins. The branches after it are not evaluated.
The placement rules
- Start with
lwc:if. Anlwc:elseiforlwc:elsemust follow a branch of the same chain. On its own it is a compile error. - Keep branches adjacent siblings. Nothing may sit between two branches of a chain. This includes other elements and text. Whitespace and HTML comments are fine.
- Order is fixed. Use one
lwc:if, then any number oflwc:elseif, then at most onelwc:else. Thelwc:elseis always last. lwc:ifandlwc:elseiftake a value. The value must be a simple property reference in braces, such as{isLoading}. It cannot be an expression like{status === 'loading'}or a negation like{!isLoading}.lwc:elsetakes no value. Writelwc:else, notlwc:else={something}.- Do not mix with the legacy directives.
if:trueandif:falseare not part of a chain. Do not put them on the same element aslwc:if.
Rule 4 is the one that surprises people coming from JavaScript. The template has no expression language, so you compute the condition in a getter.
<template>
<template lwc:if={isLoading}>
<p>Loading...</p>
</template>
<template lwc:elseif={hasError}>
<p>Something went wrong.</p>
</template>
<template lwc:else>
<p>Data is ready.</p>
</template>
</template>import { LightningElement } from 'lwc';
export default class StatusPanel extends LightningElement {
status = 'loading';
get isLoading() {
return this.status === 'loading';
}
get hasError() {
return this.status === 'error';
}
}Invalid chains and fixes
The table shows common broken chains and how to repair each one.
| Broken template | Problem | Fix |
|---|---|---|
| `<p lwc:else>` with no preceding `lwc:if` | No chain to continue | Add an `lwc:if` sibling directly before it |
| `<p lwc:if={a}>`, `<div>Note</div>`, `<p lwc:else>` | Another element splits the chain | Move the `<div>` before the chain or after it |
| `<p lwc:else={b}>` | `lwc:else` has no value | Use `lwc:elseif={b}`, or remove the value |
| `<p lwc:if={count > 0}>` | Expressions are not allowed | Create `get hasItems() { return this.count > 0; }` and use `{hasItems}` |
| `<p lwc:else>` then `<p lwc:elseif={b}>` | `lwc:else` must be last | Swap the order |
Starting a new chain
If you place a new lwc:if after a complete chain, it begins a separate chain. The two chains are evaluated independently, so one branch from each can render at the same time.
<template>
<p lwc:if={isAdmin}>Admin tools</p>
<p lwc:else>Standard view</p>
<p lwc:if={hasItems}>You have {itemCount} items.</p>
<p lwc:else>Your list is empty.</p>
</template>Exercise
A template has <p lwc:if={isOpen}>Open</p>, then <hr>, then <p lwc:else>Closed</p>. Why does it fail, and how do you fix it?
Show answer
The <hr> sits between two branches, so lwc:else has no lwc:if directly before it and the chain is broken. Move the <hr> before the lwc:if or after the lwc:else.
Exercise
You need to show a message when items.length > 0. Why does <p lwc:if={items.length > 0}> fail, and what do you write instead?
Show answer
lwc:if accepts only a simple property reference, not an expression. Define get hasItems() { return this.items.length > 0; } in the class and write <p lwc:if={hasItems}>.