Sub topic 11
Multiple templates with render()
This chapter assumes you know the basic bundle layout, the naming rules, and how a template binds to its class. It covers how one component can ship several HTML files and choose between them at runtime.
Why a bundle can hold more than one template
By default, a bundle has one HTML file whose base name matches the folder, such as greetingCard.html. The framework renders that file automatically.
Some components show very different markup depending on state. A login panel is a common case. Signed-out users see a form, and signed-in users see a welcome message. You could wrap both in lwc:if blocks in one file. When the two layouts share almost nothing, separate files are easier to read and maintain.
A bundle may contain any number of extra HTML files. Each one follows the same rules as the main template: a single root <template> element, and bindings that read from the same class.
Layout of a bundle with two templates
force-app/main/default/lwc/
└── loginPanel/
├── loginPanel.js
├── loginPanel.js-meta.xml
├── signedIn.html
└── signedOut.htmlThe extra files do not need to match the folder name. In this layout there is no loginPanel.html at all. The class decides which file to use.
Choosing a template with render()
Import each HTML file as a module. The default export of an HTML file is a template object. Then define a render() method on the class that returns one of them.
import { LightningElement } from 'lwc';
import signedIn from './signedIn.html';
import signedOut from './signedOut.html';
export default class LoginPanel extends LightningElement {
isSignedIn = false;
render() {
return this.isSignedIn ? signedIn : signedOut;
}
}The framework calls render() when the component first renders. It calls it again whenever a reactive value that affects rendering changes. Fields declared on the class are reactive, so changing isSignedIn triggers a new render and the framework picks the template that render() returns.
Both templates can use the same properties and methods from the class. Each file only uses the ones it needs.
Common errors
- Returning a string or a name.
render()must return the imported template object, not the file name.return 'signedIn'fails. - Forgetting the import. If you reference
signedInwithout importing it, you get a reference error when the component renders. - Wrong relative path. Use
./signedIn.html. Without the./, the bundler treats the path as a module name and cannot resolve it. - Expecting CSS per template. A bundle has one CSS file, which matches the component name. It applies to whichever template is rendered. To style a template separately, use a distinct class name in each HTML file.
- Side effects in render(). Keep
render()free of assignments and data loading. It can run many times, so it should only read state and return a template.
When to use this pattern
Use separate templates when the branches differ in structure, not just in a few elements. If only one paragraph changes, a conditional block in a single file is simpler. Each extra file adds an import and one more place to edit when the shared data changes.
Exercise
A component bundle has the files statusView.js, loading.html, and ready.html, and no statusView.html. What must statusView.js contain for the component to render, and what does render() return?
Show answer
It must import both templates (import loading from './loading.html'; and import ready from './ready.html';) and define a render() method on the class. The method returns one of the imported template objects, for example return this.isLoaded ? ready : loading;. It returns the template object, not a string. Without render(), the framework looks for statusView.html, which does not exist.
Exercise
A developer changes isSignedIn from false to true, but the page still shows the signed-out markup. Name one likely cause related to this chapter.
Show answer
Likely causes include a render() method that returns the wrong template or reads a value that is not reactive, or a render() that returns the same imported object on both branches. Check that isSignedIn is a class field and that the ternary returns signedIn when it is true.