Sub topic 3
The HTML Template
Prerequisites
This chapter assumes you know basic HTML and JavaScript classes, and that you have a component folder such as greetingCard in a Salesforce DX project. You don't need to know the other bundle files yet. The template reads values from the JavaScript class, and a later chapter covers that link in detail.
What the template does
The HTML file defines the markup a component renders. It lives in the bundle folder and shares the bundle's name, so greetingCard has greetingCard.html. Without this file, a component has nothing to draw, unless it overrides render() to return a template from another file. That case comes in a later chapter.
The root template element
Every template file has exactly one root element, <template>. All markup goes inside it.
<template>
<lightning-card title="Greeting">
<p class="greeting">Hello, {name}!</p>
</lightning-card>
</template>The browser never shows the <template> tag itself. The framework replaces it with your markup and attaches that markup to the component's shadow root. The component's own tag, such as <c-greeting-card>, is the host element that holds it.
Binding values with braces
Curly braces insert a value from the component class into the markup. In the example above, {name} reads the name property of the GreetingCard class.
import { LightningElement, api } from 'lwc';
export default class GreetingCard extends LightningElement {
@api name = 'World';
}The rules for braces are strict:
- Write
{name}with no quotes around it when you bind an attribute, as intitle={heading}. - Put a property or getter name inside the braces. Expressions such as
{name + '!'}or{count > 1}are not allowed. Compute the value in a getter instead. - The name must exist on the class. A misspelled name renders nothing and raises no error in the page.
Mixing template elements
A template can contain standard HTML elements, base Lightning components such as lightning-card, and other custom components. Custom components use the c- prefix for the default namespace, and camelCase folder names become kebab-case tags. The folder greetingCard becomes <c-greeting-card>.
| Element kind | Example | Notes |
|---|---|---|
| Standard HTML | `<p>`, `<div>` | Rendered as written |
| Base component | `<lightning-card>` | Provided by Salesforce |
| Custom component | `<c-greeting-card>` | Your own bundle |
Rendering on a schedule
The framework re-renders the template when a value it reads changes. The Jest test in this tutorial waits one microtask before checking the text, for this reason: the update is not instant.
Common errors
- Wrong file name. If the file is
greeting-card.htmlorGreetingCard.html, the bundle does not match the folder name and the component does not resolve. UsegreetingCard.html. - Quoted braces. Writing
title="{heading}"passes the literal text, not the value. Remove the quotes. - Expressions in braces. Move any logic into a getter on the class and bind the getter's name.
Exercise
A teammate writes <p>Hello, {name.toUpperCase()}!</p> in greetingCard.html and the component fails to compile. What is wrong, and how do you fix it?
Show answer
Braces accept only a property or getter name, not a method call or other expression. Add get upperName() { return this.name.toUpperCase(); } to the class and write <p>Hello, {upperName}!</p>.
Exercise
Why does <c-greeting-card> appear as the tag for the folder named greetingCard?
Show answer
The c is the default namespace, and the camelCase folder name is converted to kebab-case, giving c-greeting-card.