Sub topic 3
Why Templates Cannot Contain Expressions
This chapter assumes you can write modern JavaScript (classes, arrow functions, template literals) and have created a Lightning Web Component bundle with its HTML, JS, and meta XML files. You should also know the {property} binding syntax from the previous chapter.
The rule
An LWC template can reference a property of the component class inside curly braces. It cannot evaluate JavaScript. Everything between { and } must be a plain property path such as {greeting} or {contact.label}.
These all fail to compile or do not behave as you expect:
<!-- Not valid in an LWC template -->
<template>
<p>{quantity * unitPrice}</p>
<p>{name.toUpperCase()}</p>
<p>{items.length > 0 ? 'Full' : 'Empty'}</p>
<p>{!isActive}</p>
</template>Arithmetic, method calls, comparisons, ternaries, negation, and template literals are all expressions. None of them are allowed.
Why LWC works this way
LWC compiles each template into a JavaScript function ahead of time. The compiler reads every binding and records which property it points to. A plain property reference is simple to analyze, easy to validate, and fast to run. Arbitrary expressions would require the compiler to understand and sandbox any JavaScript you could write there.
The restriction also keeps responsibilities separate:
- The template describes structure: which elements exist and where values appear.
- The JavaScript class holds logic: calculations, conditions, and formatting.
Logic that lives in the class can be read in one place, tested with ordinary JavaScript tools, and reused by several parts of the template.
The fix: move the logic into a getter
A getter is a class member declared with get. It runs when read and returns a value. Reading this.total looks like reading a field, so the template binds to it exactly as it would to a field.
// orderSummary.js
import { LightningElement } from 'lwc';
export default class OrderSummary extends LightningElement {
quantity = 3;
unitPrice = 4.5;
get total() {
return this.quantity * this.unitPrice;
}
}<!-- orderSummary.html -->
<template>
<p>Total: {total}</p>
</template>The template now contains only {total}. The multiplication happens in JavaScript. When quantity or unitPrice is reassigned, the component re-renders and the template reads total again, so the displayed value stays current.
Rewriting the invalid examples
Each invalid expression above becomes a getter with a name that says what it means.
| Invalid in the template | Getter in the class |
|---|---|
| `{quantity * unitPrice}` | `get total() { return this.quantity * this.unitPrice; }` |
| `{name.toUpperCase()}` | `get upperName() { return this.name.toUpperCase(); }` |
| `{items.length > 0 ? ...}` | `get hasItems() { return this.items.length > 0; }` |
| `{!isActive}` | `get isInactive() { return !this.isActive; }` |
Negation is a common surprise. If you need the opposite of a boolean, write a getter for it. The same applies to conditions: lwc:if accepts a property or getter, not a comparison, so the condition is computed in JavaScript. The next chapters build on this.
Common errors
- Writing an expression in braces. The compiler reports an error on the template. Move the logic to a getter and bind to its name.
- Putting quotes around the binding. Write
value={name}, notvalue="{name}". Quoted braces are treated as literal text in current LWC templates. - Binding to a method.
{formatName}does not call a method namedformatName. Use a getter, which is read like a property. - Forgetting
this. Inside the getter, fields are reached withthis.. The template does not usethis.
Exercise
A teammate writes <p>{firstName + ' ' + lastName}</p> in a template and the component does not compile. Show the fix, and explain why the template needs no further change when lastName is later reassigned.
Show answer
Add a getter to the class: get fullName() { return ${this.firstName} ${this.lastName}; }, and change the template to <p>{fullName}</p>. The template cannot evaluate expressions, so the concatenation moves into JavaScript. When lastName is reassigned, the component re-renders, and the template reads fullName again, which runs the getter and returns the new value.