Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 28 ยท Alpine.js Basics

Conditionals and Loops with x-if and x-for

Render conditionally with x-if and repeat markup with x-for in Alpine.js: template tags, keys, index, nested loops, filtering with a getter and common mistakes.

So far you have shown and hidden elements with x-show, and every element existed in the HTML from the start. Two directives let Alpine create markup instead: x-if adds or removes an element depending on a condition, and x-for repeats an element once per item in an array. Both must be placed on a <template> tag, which is the reason this lesson starts with it.

Why template tags

A <template> element is inert HTML: the browser parses it but does not display it, and does not run anything inside it. Alpine uses it as a blueprint. It reads the single element inside the template and stamps out real copies next to it, or none at all. That is why you will always see this shape:

HTML
<template x-if="condition">
  <div>...exactly one root element...</div>
</template>

The template must contain exactly one root element. Two siblings inside it will not both render.

x-if versus x-show

Both react to a condition, but they work differently.

x-show or x-if?

x-show Stays in the DOM and toggles display.
x-if Added and removed from the DOM.

Toggle often? x-show. Costly or rare? x-if.

1 / 2
x-show keeps it, x-if rebuilds it

The x-show input survives because it never left the page. The x-if input was destroyed and rebuilt. Use x-if for content that is expensive to create or that should reset, and x-show for anything toggled often.

Looping with x-for

x-for has the form item in items. Alpine copies the template's element once per item, and inside the copy the variable item is available.

A fruit list with x-for

Index and key

To get the position, use (item, index) in items. The index starts at 0.

The :key attribute tells Alpine how to identify each row, so when the list changes it can move existing elements instead of rebuilding them. Give it a value that is unique and stable, such as a database id. Do not use the index when rows can be reordered or deleted, because the same index will point at a different item afterwards.

Objects with an id as the key

Filtering with a getter

Alpine has no built-in filter. The idiom is a getter that returns the filtered array, and x-for loops over the getter. Because getters are reactive, the list updates when the search text or the data changes.

Search a list with a getter

The empty-state message uses x-show on the getter's length. That pairing, a loop plus an empty message, appears in nearly every list UI.

Nested loops

A template can contain another template. The inner loop can read the outer variable.

A nested loop: groups and items

The outer template's single root is the <div>, which contains the heading and the inner template. Make the inner key unique across the whole list (here by prefixing the group name).

Common mistakes

  • No <template> tag. x-for and x-if on a normal element do nothing useful. Always use a template.
  • More than one root inside the template. Wrap siblings in a single <div> or other element.
  • A missing or non-unique :key. Duplicate keys give confusing behavior; use ids.
  • Using the index as key for editable lists. Rows that are removed or reordered will keep the wrong state.
  • Mutating the array in a way Alpine cannot see. Methods such as push, splice and assigning a new array are all tracked, so you are safe, but replacing individual items inside a plain copy that you never assign back is not.
  • Putting x-if and x-for on the same template. Combine them by nesting, or filter with a getter.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. On which element must x-for and x-if be placed?

    Alpine uses the inert template as a blueprint and renders one real element from it per item or condition.
  2. 2. What is the difference between x-show and x-if?

    x-show leaves the element in the DOM (display: none), while x-if creates and destroys it, losing its internal state.
  3. 3. Which value is the best :key for rows that can be deleted and reordered?

    A stable id lets Alpine match each row to its data even when positions change.
  4. 4. How do you filter the list shown by an x-for in Alpine?

    A getter is reactive, so looping over it updates automatically when the search text or data changes.

Summary

  • x-if and x-for go on a <template> that contains exactly one root element.
  • x-show toggles display; x-if adds and removes the element and resets its internal state.
  • x-for="(item, index) in items" :key="item.id" repeats markup; the key should be unique and stable.
  • Filter by looping over a getter, and pair the loop with an x-show empty message.
  • Nested templates work; keep keys unique and structure to one root per template.

Continue with Forms and x-model.