HTML Template Directives - Salesforce Lightning Component Library
Render DOM Elements Conditionally - Salesforce Lightning Component Library
Render Lists - Salesforce Lightning Component Library
The template is created using HTML. Not all elements that you code in the HTML appear to the end user, as you can use conditional rendering.
lwc:if
and for:each
, which give you more power to manipulate the DOM in markup).The following is an example of the translation that LWC provides:
<template>
<div class="slds-var-m-around_medium">
My First LWC!!
</div>
</template>
Is translated to:
<c-my-first-lwc>
<div class="slds-var-m-around_medium">
My First LWC!!
</div>
</c-my-first-lwc>
for:each
, iterator:iteratorName
, lwc:if
, lwc:else
, lwc:elseif
, or if:true|false
.Look at the following example for a template and a Javascript files, where the template uses a property:
<template>
<p>Hello, {greeting}!</p>
<lightning-input label="Name" value={greeting} onchange={handleChange}>
</lightning-input>
</template>
import { LightningElement } from ‘lwc’;
export default class HelloBinding extends LightningElement {
greeting = 'World';
handleChange(event) {
this.greeting = 'touché' ; //<-- The value in the event
}
}
greeting
property in the template is bound to the greeting
property in the JavaScript class.