Description: The x-if directive allows you to completely add or remove a block of HTML from the DOM based on a JavaScript data condition. A crucial requirement is that the HTML content to be conditionally rendered must be nested within a <template> tag.
The core syntax for x-if is:
<template x-if="yourCondition">
<div>
Content to be conditionally rendered.
This could be a simple div, a complex component, or any valid HTML.
</div>
</template>
<template>: This HTML tag is a container for holding HTML content that is not rendered by default. AlpineJS uses it as a blueprint.x-if="yourCondition": This AlpineJS directive is placed on the <template> tag. yourCondition is a JavaScript expression that evaluates to a boolean (true or false).<template>: This is the HTML block that AlpineJS will clone and insert into the DOM immediately after the <template> tag if yourCondition is true. If yourCondition is false, this content (and any previously rendered instance of it) is completely removed from the DOM.x-if content in a <template> tag:
A very common mistake is attempting to place x-if directly on the element you wish to conditionally render, for example: <div x-if="isVisible">Content</div>. This will not work as intended for conditionally adding/removing the div. AlpineJS specifically requires x-if to be on a <template> tag. The template's _contents_ are then managed by AlpineJS.
Correct usage:
<template x-if="isVisible">
<div>This content is correctly managed.</div>
</template>
x-if with x-show:
Both directives control visibility, but they do so very differently:
x-if: Removes elements from the DOM when the condition is false, and adds them back (re-creates them) when true. This means the elements, their event listeners, and any associated Alpine component state within them are destroyed and re-initialized.x-show: Only toggles the CSS display: none; style. The elements remain in the DOM, just hidden or visible. State is preserved.When to use x-if:
When to use x-show:
x-if block is destroyed when hidden:
Since x-if removes elements from the DOM, any Alpine.js components (elements with x-data) defined inside an x-if block are completely destroyed when the condition becomes false. When the condition turns true again, the HTML is re-inserted, and any Alpine components within it are re-initialized from their original `x-data` definition. This means any dynamic state (like user input in a form field within the x-if block) is lost. If you need to preserve such state, x-show is usually the better choice.
Click the button to toggle the visibility of an "Advanced Details" section.
This section is managed by x-if and will be completely added or removed from the DOM.
This section contains sensitive or complex settings. It's completely removed from the DOM when not visible to optimize performance and simplify the UI. You can verify this using your browser's developer tools (inspect elements).
Nested Component Example:
This demonstrates state destruction. Its state (input value and init count) is reset each time this "Advanced Details" section is shown.
Current input:
Initialization count:
Try typing in the input field above, then hide and show this section. The input will be cleared, and the "Initialization count" will increment, proving the nested component was re-initialized.