Sub topic 8
Templates and Data Binding
This chapter assumes you know HTML, JavaScript classes, and how the browser DOM and event listeners work. It also assumes you have seen the component bundle layout from the previous chapter: one folder holding an HTML file, a JavaScript file, and optional CSS.
What a template is
An LWC template is the HTML file in the bundle. It describes what the component renders. The file has one root <template> element, and everything inside it becomes the content of the component's shadow root.
The template is declarative. It does not run code. It points at things on the component class, and the framework reads them when it renders.
Binding values with expressions
Curly braces bind a template to the component class. In {name}, name must be a field, getter, or method-free property on the class.
<!-- helloGreeting.html -->
<template>
<p>Hello, {name}!</p>
<input type="text" value={name} onchange={handleChange} />
</template>// helloGreeting.js
import { LightningElement } from 'lwc';
export default class HelloGreeting extends LightningElement {
name = 'World';
handleChange(event) {
this.name = event.target.value;
}
}Two kinds of binding appear here:
- Text binding:
{name}inside an element puts the field's value into the text. - Attribute and property binding:
value={name}has no quotes around the braces. Quoting it would make it a plain string.
The onchange={handleChange} binding attaches an event listener. The on prefix plus a standard event name connects the event to a class method.
Data flows one way
Data moves from the class to the template. When the user types in the input, the template does not update the field. Your handler does that:
handleChange(event) {
this.name = event.target.value;
}Reassigning name makes the framework re-render the parts of the template that use it. The next chapter covers how that works.
Using getters for computed values
Expressions in the template cannot contain logic. {name.toUpperCase()} and {count + 1} are not allowed. Put the logic in a getter instead.
import { LightningElement } from 'lwc';
export default class ShoutGreeting extends LightningElement {
name = 'World';
get shoutedName() {
return this.name.toUpperCase();
}
}<template>
<p>Hello, {shoutedName}!</p>
</template>Common errors
| Mistake | What happens | Fix |
|---|---|---|
| `value="{name}"` with quotes | Compiler error or a literal string, depending on version | Write `value={name}` |
| `{name.toUpperCase()}` | Compile error | Use a getter |
| Binding to a name that is not on the class | Renders nothing | Check spelling and that the field exists |
| Expecting typing to update `name` automatically | The field never changes | Write a handler that assigns it |
Exercise
A template contains <p>Total: {count + 1}</p>. Why does this fail, and how do you fix it?
Show answer
Template expressions can only reference a property on the class, not run operations. Add a getter such as get nextCount() { return this.count + 1; } to the class and write {nextCount} in the template.