AI • LWC • Salesforce •

Salesforce Aura Tag – Conditional Rendering in Lightning Aura Components

khumed Khatib 5 min read
Salesforce Aura:if Tag – Conditional Rendering in Lightning Components

Salesforce Aura:if Tag – Conditional Rendering in Lightning Aura Components

In Salesforce Lightning Aura Components, the <aura:if> tag is used to conditionally display content based on a Boolean expression. It allows developers to dynamically show or hide UI elements depending on whether a particular condition evaluates to true or false.

If you have worked with conditional statements such as if-else in Java, JavaScript, or Apex, understanding the Aura:if tag will be very easy.

1. What is the Aura:if Tag in Salesforce?

The <aura:if> tag works as a conditional rendering mechanism in Salesforce Aura Components. It evaluates the expression provided in its isTrue attribute.

  • If the condition is true, the main content is displayed.
  • If the condition is false, the content inside the else block is displayed.

For example, imagine an Account Status Indicator where you want to display a green icon for an Active Account and a red icon for an Inactive Account. You can implement this functionality using <aura:if>.

2. Syntax of Aura:if

Let's understand the basic syntax of the Aura:if tag.

<aura:component>

    <aura:if isTrue="{!v.truthy}">

        True

        <aura:set attribute="else">
            False
        </aura:set>

    </aura:if>

</aura:component>

Explanation

  • isTrue: Specifies the Boolean expression that Salesforce evaluates.
  • True: Content displayed when the condition evaluates to true.
  • aura:set attribute="else": Defines the content displayed when the condition evaluates to false.

3. Practical Examples of Aura:if

Example 1: Using a Boolean Attribute

We can evaluate the isTrue expression by passing a Boolean value through a component attribute.

<aura:component>

    <aura:attribute
        name="imyValue"
        type="Boolean"
        default="false" />

    <aura:if isTrue="{!v.imyValue}">

        True

        <aura:set attribute="else">
            False
        </aura:set>

    </aura:if>

</aura:component>

Explanation:

  • We created a Boolean attribute named imyValue.
  • Its default value is false.
  • The Aura:if tag evaluates the value of this attribute.
  • Since the default value is false, the output will be False.

Example 2: Evaluating an Expression Directly

We can also evaluate an expression directly inside the isTrue attribute instead of using a Boolean attribute.

<aura:component>

    <aura:attribute
        name="imyValue"
        type="Integer"
        default="10" />

    <aura:if isTrue="{!v.imyValue == 10}">

        True

        <aura:set attribute="else">
            False
        </aura:set>

    </aura:if>

</aura:component>

Explanation:

  • The component attribute imyValue has a default value of 10.
  • The expression checks whether imyValue == 10.
  • Since the condition is true, Salesforce displays True.

Example 3: Hide or Display a DIV Using Aura:if

One of the most common uses of Aura:if is to conditionally display HTML elements such as a DIV.

<aura:component>

    <aura:attribute
        name="imyValue"
        type="Boolean"
        default="false" />

    <aura:if isTrue="{!v.imyValue}">

        <div>
            Hello World
        </div>

    </aura:if>

</aura:component>

Explanation:

  • We created a Boolean attribute with a default value of false.
  • The DIV element is placed inside the Aura:if block.
  • When the value becomes true, Hello World is displayed.
  • When the value is false, the DIV is not rendered.

4. Real-Time Example: Display Account Status Using Aura:if

Let's consider a practical business scenario. We want to display an Account status indicator. If the Account is Active, show a green status message. Otherwise, show a red status message.

<aura:component>

    <aura:attribute
        name="isActive"
        type="Boolean"
        default="true" />

    <aura:if isTrue="{!v.isActive}">

        <div style="color:green;">
            ● Account is Active
        </div>

        <aura:set attribute="else">

            <div style="color:red;">
                ● Account is Inactive
            </div>

        </aura:set>

    </aura:if>

</aura:component>

In this example, the Account status is controlled by the Boolean attribute isActive. Based on its value, the component displays the appropriate status message.

5. Important Points to Remember

  • The <aura:if> tag is used for conditional rendering in Aura Components.
  • The isTrue attribute accepts an expression that evaluates to a Boolean value.
  • The else block is optional.
  • Content inside the conditionally selected branch is rendered, while the other branch is not rendered.
  • When the condition changes, Aura updates the component accordingly.
  • Use Aura:if when you want to conditionally create or remove UI elements rather than simply hide them using CSS.

Note: Conditional rendering using Aura:if is different from CSS-based visibility. With CSS, an element may remain in the DOM but become invisible. Aura:if conditionally creates and destroys its content.

6. Aura:if Interview Questions

Q1. What is the purpose of the Aura:if tag?

The Aura:if tag is used to conditionally render content in Salesforce Lightning Aura Components based on a Boolean expression.

Q2. What is the purpose of the isTrue attribute?

The isTrue attribute evaluates a Boolean expression to determine whether the main content or else content should be displayed.

Q3. Is the else block mandatory in Aura:if?

No. The else block is optional. If the condition is false and no else block is provided, nothing is rendered from that Aura:if block.

Q4. What is the difference between Aura:if and CSS display:none?

Aura:if conditionally creates and destroys component content, whereas CSS display:none hides an element without removing it from the DOM.

Q5. Can we use expressions inside the isTrue attribute?

Yes. You can use supported Aura expressions, including Boolean comparisons, inside the isTrue attribute.

7. Conclusion

The Aura:if tag is a simple but powerful feature of the Salesforce Aura Framework. It helps developers build dynamic user interfaces by displaying content based on conditions.

Whether you want to show or hide a DIV, display status indicators, or conditionally render component sections, Aura:if provides a straightforward solution.

Keep practising these concepts to strengthen your Salesforce Aura Component development skills.