Template expressions support a wide range of computations within a component template. Use template expressions to reduce or eliminate the need for display-only properties or getters.
The Limits of Basic Property Binding
With simple property binding, dynamically computing a value for a property used in a template requires you to:
Define a getter that computes the value in the component class, and then
Reference that getter in the template.
1<template>2 {propertyName}3</template>
1import{LightningElement}from "lwc";2export default class Component extends LightningElement{3 get propertyName(){4 // Compute a value for propertyName5}6}
Template expressions are more powerful than basic property binding. They can contain any JavaScript expression that is valid in a template context. Additionally, complex expressions can be used for inline computations, which reduces the need for getters that solely compute or format values for a component’s user interface.
Compute and Format Values for Display Using Expressions
Template expressions are more powerful than basic property binding. They can contain any JavaScript expression that is legal in a template context. Complex expressions can be used for inline computations, which reduces the need for getters that solely compute or format values for a component’s user interface.
The following example illustrates a range of template expressions used to display user and status information. Additional examples covering specific syntax and use cases are offered throughout the documentation.
1import{LightningElement}from "lwc";23export default class ExampleComponent extends LightningElement{4 name = "World";5 isLoggedIn = true;6 user = {name: "John Doe"};7 error = null;8 timestamp = Date.now();9 firstName = "John";10 lastName = "Doe";1112 formatDate(timestamp){13 return new Date(timestamp).toLocaleDateString();14}15}
Release Preview
This release is in preview. Features described here don't become generally available until the latest general availability date that Salesforce announces for this release. Before then, and where features are noted as beta, pilot, or developer preview, we can't guarantee general availability within any particular time frame or at all. Make your purchase decisions only on the basis of generally available products and features.