Lightning web component Conditional Rendering

Let’s discuss here how to use the lightning web component HTML template if:true|false directive to display conditional data. The power of Lightning Web Components is the templating system, which uses the virtual DOM to render components smartly and efficiently. As the name suggests, the attribute directives are altering the properties of an element to which they are attached and the structural ones are changing. Directives are special HTML attributes, like if:true and for:each, that gives you more power to manipulate the DOM in markup.HTML templates also render data to the DOM.  Please refer to this link for the development environment setup 

To render HTML conditionally, add the if:true|false directive to a nested <template> tag that encloses the conditional content.The if:true|false={property} directive binds data to the template and removes and inserts DOM elements based on whether the data is a truthy or falsy value.

1. Create Lightning web component

create a lightning web component using the following SFDX command. In this component, we will be showing the text value based on the selected checkbox.

Here is the ConditionalRendering.html code

Use this code in ConditionalRendering.js

Use this code ConditionalRendering.js-meta.xml code


2. Push changes to scratch org.

Push changes to scratch using this command and add this component to record page using lighting app builder

And you can able to see the below output and based on the selected checkbox component will present the text to UI.


Understand the Code

The following code will return the picklist values from the Account rating field and we will be using this wire adapter data show in the UI.


The following markup is converted the picklist values into the select checkboxes and onchange method will update the selected values 

The following markup is used to render the data based on selected values.