When we create multiple html file within a single component is called multiple template component. To reference CSS from an extra template, the CSS filename must match the filename of the extra template.
Template loaded by render method replace the complete template.
Let see how this whole process work with the below image
<template>
<lightning-card title="Render Multiple Template" icon-name="custom:custom14">
<div class="slds-m-around_medium">
<h2 class="slds-text-heading_medium slds-m-bottom_medium">Choose your option</h2>
<lightning-button variant="destructive" label="Sign Up" title="Sign Up" onclick={handleClick}
class="slds-m-left_x-small"></lightning-button>
<lightning-button variant="success" label="Sign In" title="Sign In" onclick={handleClick}
class="slds-m-left_x-small"></lightning-button>
</div>
</lightning-card>
</template>
import { LightningElement } from 'lwc';
import signinTemplate from './signinTemplate.html'
import signupTemplate from './signupTemplate.html'
import defaultTemplate from './renderMultipleDemo.html'
export default class RenderMultipleDemo extends LightningElement {
selected = null;
render(){
return this.selected === 'Sign Up' ? signupTemplate:
this.selected === 'Sign In'? signinTemplate:
defaultTemplate
}
handleClick(event){
this.selected = event.target.label
}
submitHandler(event){
if(event.target.label === 'Sign In'){
console.log("Sign In Successfully")
} else if (event.target.label === 'Sign Up'){
console.log("Sign Up Successfully")
} else {
}
}
}
<template>
<lightning-card title="Render Multiple Template" icon-name="custom:custom14">
<div class="slds-m-around_medium">
<h2 class="slds-text-heading_medium slds-m-bottom_medium">Sign in Form</h2>
<lightning-input type="text" label="Enter your username"></lightning-input>
<lightning-input type="password" label="Enter your password"></lightning-input>
<lightning-button variant="success" label="Sign In" title="Sign In" onclick={submitHandler}></lightning-button>
<lightning-button variant="destructive" label="Back" title="Back" onclick={handleClick}></lightning-button>
</div>
</lightning-card>
</template>
<template>
<lightning-card title="Render Multiple Template" icon-name="custom:custom14">
<div class="slds-m-around_medium">
<h2 class="slds-text-heading_medium slds-m-bottom_medium">Sign up Form</h2>
<lightning-input type="text" label="Enter your first name"></lightning-input>
<lightning-input type="text" label="Enter your last name"></lightning-input>
<lightning-input type="email" label="Enter your Email address"></lightning-input>
<lightning-input type="password" label="Enter your password"></lightning-input>
<lightning-button variant="success" label="Sign Up" title="Sign Up" onclick={submitHandler}>
</lightning-button>
<lightning-button variant="destructive" label="Back" title="Back" onclick={handleClick}>
</lightning-button>
</div>
</lightning-card>
</template>
h2{
font-size:20px;
color:red;
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>48.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__AppPage</target>
<target>lightning__RecordPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
After placing the component on the page, you will see the following output.