Sub topic 5
Using lwc:if on `<template>` and on elements
This chapter assumes you can read an LWC HTML template and its JavaScript class. It also assumes you know how to bind a property with curly braces and what a getter is. You will learn where lwc:if can go: on a <template> tag or directly on an element.
Two places for the same directive
The lwc:if directive can be placed in two ways.
- On a
<template>tag. The template tag is a wrapper. It adds nothing to the DOM. Only its children are rendered. - On an element such as
<p>or<div>. The element itself is rendered or removed.
Both forms evaluate the same expression and follow the same rules. The only difference is what ends up in the DOM.
Wrapping several elements
Use a <template> tag when one condition controls more than one element. The wrapper avoids an extra <div> that you would otherwise have to style or work around.
<template>
<template lwc:if={isLoggedIn}>
<h2>Welcome back</h2>
<p>Your last visit was {lastVisit}.</p>
<button onclick={handleLogout}>Log out</button>
</template>
</template>When isLoggedIn is truthy, the heading, paragraph, and button appear as direct children of the component's rendered output. When it is falsy, none of them exist in the DOM.
Controlling a single element
When only one element is conditional, put the directive on it. This is shorter and easier to read.
<template>
<p lwc:if={hasItems}>You have {itemCount} items.</p>
<p lwc:else>Your list is empty.</p>
</template>The same applies to lwc:elseif and lwc:else. Each branch can use either form, and you can mix them in one chain.
<template>
<p lwc:if={isLoading}>Loading...</p>
<template lwc:elseif={hasError}>
<p>Something went wrong.</p>
<button onclick={handleRetry}>Retry</button>
</template>
<p lwc:else>Data is ready.</p>
</template>Here the first and last branches are single elements. The middle branch needs two elements, so it uses a template wrapper.
The matching JavaScript
The directives accept a property reference only. A comparison such as status === 'error' does not work in the template. Put it in a getter.
import { LightningElement } from 'lwc';
export default class StatusPanel extends LightningElement {
status = 'loading';
get isLoading() {
return this.status === 'loading';
}
get hasError() {
return this.status === 'error';
}
handleRetry() {
this.status = 'loading';
}
}Getters are covered in the next chapter. For now, note that the template only sees isLoading and hasError.
How the choice shows up in the DOM
Choosing between the two forms
| Situation | Use |
|---|---|
| One element is conditional | Directive on the element |
| Several sibling elements share one condition | Directive on a `<template>` tag |
| You want no extra wrapper in the DOM | Directive on a `<template>` tag |
| The element needs its own class or attributes | Directive on the element |
Common mistakes
- Wrapping several elements in a
<div>just to hold the directive. A<template>tag does the same job without adding a node. - Putting an expression in braces, such as
{count > 0}. Move it to a getter. - Placing
lwc:elseon an element that is not the next sibling of the previous branch. Chain rules are covered later, but adjacency matters.
Exercise
A component must show a heading and a paragraph together when isReady is truthy. You want no extra wrapper element in the DOM. Where should lwc:if={isReady} go, and why?
Show answer
Put it on a <template> tag that contains the heading and the paragraph. The template tag is not rendered, so only the heading and paragraph are added to the DOM. Putting the directive on a <div> would add that <div> to the DOM as well.
Exercise
What is wrong with <p lwc:if={items.length > 0}>Items found</p>, and how do you fix it?
Show answer
The directive takes a property reference, not an expression. Add a getter such as get hasItems() { return this.items.length > 0; } and write <p lwc:if={hasItems}>Items found</p>.