HTML Template Directives in LWC
September 20, 2023
<template> HTML Template Directives
1- for:each={Array and Map}
- for:item=”currentItem”
- for:index=”index”
iteration.html
<template>
<lightning-card title="Implement Iteration for:each directives in LWC" icon-name="custom:custom15">
<div class="slds-m-around_medium">
<template for:each={data} for:item="item" for:index="index">
<!-- As Id of the each record is unique so put in key-->
<div key={item.Id}>
<p>Sl No : {index}</p>
<p>Name : {item.Name}</p>
<p>Email : {item.Email}</p>
<hr />
</div>
</template>
</div>
</lightning-card>
</template>
iteration.js
import {LightningElement} from 'lwc';
export default class Iteration extends LightningElement {
data = [{
Id: 1,
Name: 'Test1',
Email: 'test1@gmail.com',
},
{
Id: 2,
Name: 'Test2',
Email: 'test2test@gmail.com',
},
{
Id: 3,
Name: 'Test3',
Email: 'test3t@gmail.com',
},
];
}
2- iterator:iteratorName={array}
- value
- index
- first
- last
iterator.html
<lightning-card title="Implement Iteration for:each directives in LWC">
<div class="slds-m-around_medium">
<template iterator:it={data}>
<div key={it.value.Id}>
<div if:true={it.first} class="list-first">
First Item
</div>
<div if:true={it.last} class="list-last">
Last Item
</div>
{it.value.Name}, {it.value.Email}
<hr />
</div>
</template>
</div>
</lightning-card>
iterator.js
import { LightningElement } from 'lwc';
export default class Iteration extends LightningElement {
data = [
{
Id: 1,
Name: 'Test1',
Email: 'test1@gmail.com',
},
{
Id: 2,
Name: 'Test2',
Email: 'test2test@gmail.com',
},
{
Id: 3,
Name: 'Test3',
Email: 'test3t@gmail.com',
},
];
}
3- if:true|false={expression}
lwcIfElse.html
<template>
<lightning-card title="TemplateIFTrueConditionalRendering" icon-name="custom:custom14">
<div class="slds-m-around_medium">
<lightning-input type="checkbox" label="Show details" onchange={handleChange}></lightning-input>
<template if:true={isVisible}>
<div class="slds-m-vertical_medium">
This will display if isVisible will be true!
</div>
</template>
<template if:false={isVisible}>
<div class="slds-m-vertical_medium">
This will display if isVisible will be false!
</div>
</template>
</div>
</lightning-card>
</template>
lwcIfElse.js
import { LightningElement } from 'lwc';
export default class LwcIfElse extends LightningElement {
isVisible = false;
handleChange(event) {
this.isVisible = event.target.checked;
}
}
5
2
votes
Article Rating
Subscribe
Login
0 Comments
Oldest
Newest
Most Voted
Inline Feedbacks
View all comments
Latest Post
About Me
Welcome to an Aimer's weblog :)
Hi! Asif Parvez here, I'm from West Bengal's Howrah. I have extensive experience in Apex, Integration (REST API), LWC, ADMIN and working as Senior Salesforce Developer for Dazeworks Technologies(An iLink Digital Company). I am also a content creator and blogger.
I have three certifications(PD-I, PD-II, Salesforce Associate).
Our Visitor
Our Visitor
0
0
1
1
3
9
Users Today : 0
Users Yesterday : 0
Users Last 7 days : 11
Users Last 30 days : 65
Users This Month : 0
Users This Year : 226
Total Users : 1139
Views Today :
Views Yesterday :
Views Last 7 days : 13
Views Last 30 days : 107
Views This Month :
Views This Year : 347
Total views : 1934
Who's Online : 0
Your IP Address : 216.73.216.50
Server Time : May 2, 2026 8:54 amPowered By WPS Visitor Counter