Description: The x-if directive in AlpineJS allows you to completely add or remove a block of HTML from the Document Object Model (DOM) based on a JavaScript expression evaluating to true or false. Crucially, the HTML content that you want to conditionally render must be nested within a <template> tag.
The core syntax for x-if is straightforward:
<template x-if="condition">
<div>
This content is only in the DOM if 'condition' is true.
</div>
</template>
<template> tag: This HTML tag is essential. x-if must be placed on a <template> tag. The <template> tag itself is not rendered in the DOM; it serves as an invisible container or blueprint for the content within it.x-if="condition": This is the AlpineJS directive. The condition is any JavaScript expression that resolves to a boolean value (true or false). This expression can access data properties from your Alpine component.<template>: This is the actual HTML block that AlpineJS will add to the DOM when the condition is true, and completely remove from the DOM when the condition is false.Forgetting to wrap x-if content in a <template> tag:
You might be tempted to write <div x-if="myCondition">...</div>. While AlpineJS might try to interpret this, it's not the correct or intended usage for conditionally rendering the content of the div as a template. The x-if directive is specifically designed to work with the <template> element. The <template> acts as a blueprint. If x-if is placed directly on an element like a <div>, Alpine treats that <div> itself as the template. When the condition is false, the <div> is removed entirely. This might seem to work for a single element, but it deviates from the pattern and limits its power for blocks of HTML. Always use <template x-if="..."> to ensure the content inside the template is what Alpine conditionally renders.
Confusing x-if with x-show:
Both directives control visibility, but they do so differently:
x-if: Truly removes elements from the DOM when the condition is false and adds them back (re-initializes them) when true. This is generally better if:
x-show: Only toggles the display: none; CSS style on the element. The element always remains in the DOM. This is lighter and often preferred for:
Think of x-if as creating and destroying, while x-show is like hiding and showing with a curtain.
State within an x-if block is destroyed when hidden:
Because x-if completely removes the HTML elements from the DOM, any Alpine components, internal state (like input field values), or even event listeners defined within that block are destroyed. When the condition becomes true again, the HTML is re-created from the template, and any components or state are re-initialized. If you need to preserve state across visibility changes, x-show is the appropriate directive.
This example simulates a login scenario. Clicking the button toggles a logged-in state, and x-if is used to display different content based on this state. Notice how the text input field inside the "logged-in" section resets when you log out and log back in, demonstrating state destruction.
You have access to exclusive content.
Hello, !
Please log in to view protected content.
Current state: isLoggedIn is .
When isLoggedIn is true, the welcome message block is present in the DOM. When false, it's removed from the DOM.