Sub topic 1
Templates, Data Binding and the Shape of List Data
This chapter sets up the ground that list rendering stands on. You will see how a template reads data from the component class, and what shape an array needs before a template can loop over it. Later chapters build on this with for:each and iterator.
Prerequisites
This chapter assumes you already know:
- Basic HTML and JavaScript, including arrays and the
mapmethod. - How an LWC component is structured: an HTML template and a JavaScript class that extends
LightningElement. - How to bind a class property in a template with curly braces.
- Basic conditional rendering in templates.
How a template reads data
A Lightning Web Component has two halves that share a name. The HTML file holds the <template> element. The JavaScript file holds a class. The template can read any public or private field of that class, and it can also read a getter.
You bind a value by wrapping a property name in curly braces. The braces must contain a property path and nothing more.
<!-- greeting.html -->
<template>
<p>Hello, {userName}</p>
<p>City: {address.city}</p>
</template>// greeting.js
import { LightningElement } from 'lwc';
export default class Greeting extends LightningElement {
userName = 'Amy';
address = { city: 'Leeds' };
}{userName} reads the field. {address.city} follows a dot path into an object. Both are plain lookups.
What a template cannot do
A template does not run JavaScript. An expression such as {contact.Name.toUpperCase()}, {count + 1} or {a === b} is not allowed. This limit matters for lists, because you will often want a value that depends on each item, such as a CSS class or a formatted label.
When you need a computed value, you have two options:
- Write a getter in the class for a single value, such as
get hasContacts(). - Build the value into each item in JavaScript before the template sees the list. A later chapter covers this.
For now, remember the rule: the template displays values, and the class computes them.
The shape of list data
To render a list, the template needs an array. Each element should be an object, and each object should carry a unique identifier.
// contactList.js
import { LightningElement } from 'lwc';
export default class ContactList extends LightningElement {
contacts = [
{ Id: '003A', Name: 'Amy Taylor' },
{ Id: '003B', Name: 'Ravi Patel' },
{ Id: '003C', Name: 'Lena Fischer' }
];
}This array has three properties worth noticing.
- It is an array. A single object or a string will not iterate as a list.
- Each element is an object with named fields, so the template can write
{contact.Name}. - Each object has an
Idthat no other element shares. LWC uses a unique value like this as thekeyfor each rendered item. Why the key matters is the subject of its own chapter.
Data from Salesforce usually arrives in this shape already. A record from Apex or the Lightning Data Service has an Id field and one field per column, so you rarely need to invent the identifier yourself.
Where the data comes from
The array can be defined in several ways. The template does not care which one you pick.
| Source | Example | Notes |
|---|---|---|
| Field with a literal value | `contacts = [ ... ];` | Good for learning and for fixed data. |
| Getter | `get contacts() { ... }` | Computed each time the template renders. |
| Wire service or Apex result | `@wire(getContacts) contacts;` | Data arrives later, so the value can be empty at first. |
That last row has a consequence. Real data often arrives after the first render, so a list can start empty or undefined. Your template has to cope with that, and a later chapter shows how.
From data to rendered list
The flow below shows how an array in the class becomes a list on screen.
The loop directive in step C is where for:each and iterator come in. Both read the same kind of array. They differ in what they let you do with each pass, and the next chapters cover those differences.
A first look at the result
This template previews the next chapter. It binds to the contacts array above and repeats one li per object.
<!-- contactList.html -->
<template>
<ul>
<template for:each={contacts} for:item="contact">
<li key={contact.Id}>{contact.Name}</li>
</template>
</ul>
</template>Each li reads contact.Name with the same dot-path binding you saw earlier. The only new pieces are the for:each and for:item attributes and the key. Treat them as given for now.
Exercise
A teammate writes {contact.Name.toUpperCase()} inside a list template and the component fails to compile. Why does it fail, and how would you display the name in upper case?
Show answer
Templates only allow a property path inside curly braces, so method calls are not allowed. Compute the value in JavaScript instead. Either add a property such as upperName: contact.Name.toUpperCase() to each item with map, or write a getter that returns the transformed array, then bind {contact.upperName} in the template.
Exercise
Which two features should every object in a list array have so a template can render it well?
Show answer
Named fields the template can bind to, such as Name, and a unique identifier such as Id that can serve as the item's key.