15 min Beginner
🏔️ ALPINEJS FOUNDATIONS

AlpineJS + PHPue

Client-side interactivity without the complexity. Counters, forms, tabs — the stuff users actually interact with.

🎯

What You'll Build

Three components you'll actually use in real projects

🔢
Live Counter
📝
Reactive Form
📑
Tab Switcher

Why AlpineJS with PHPue?

PHPue handles your server-side logic. But when you need a dropdown to actually open, a counter to actually count, or a modal to actually appear — that's browser territory. AlpineJS lives there.

Use AlpineJS for:

  • • Dropdowns, modals, toggles
  • • Tab switchers
  • • Character counters, live previews
  • • Form validation feedback
  • • Dark mode

Don't use AlpineJS for:

  • • Complex state management
  • • Full SPA routing
  • • x-for (needs x-ssr extension)
  • • Replacing p-directives

Simple rule: PHP owns the server. AlpineJS owns the browser. PHP handles data. AlpineJS handles clicks.

Step 1: Add AlpineJS

One line in your App.pvue header. That's it.

<header>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    
    <!-- Add this -->
    <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
    
    <link rel="stylesheet" href="/assets/css/styles.css">
</header>

Done. AlpineJS is now available in every view and component. The defer attribute means it loads after your HTML — no flickering.

Your First Component: A Counter

Click a button. Number goes up. That's reactive UI in its simplest form.

Try it:

Three parts:

1. x-data="{ count: 0 }" — Creates a reactive scope with a count variable
2. @click="count++" — Increments count on click
3. x-text="count" — Displays and auto-updates the value

Mixing PHP Data with AlpineJS

Here's where it gets good. PHP sets the initial value. AlpineJS takes over from there.

<script>
    $initialCount = 5;
</script>

<template>
    <div x-data="{ count: {{ $initialCount }} }">
        <button @click="count++">
            Count: <span x-text="count">{{ $initialCount }}</span>
        </button>
    </div>
</template>

What's happening:

1. PHP renders {{ $initialCount }} as 5 on the server

2. Browser receives x-data="{ count: 5 }"

3. AlpineJS takes over — count is now reactive client-side

4. The fallback between x-text tags shows "5" before AlpineJS loads

Always include a fallback value between x-text tags. It renders instantly — no flicker while AlpineJS initializes.

Reactive Form with Live Preview

x-model gives you two-way binding. Type in a field, see it update instantly. No JavaScript required.

Type below and watch the preview update:

Live Preview

Name:

Email:

Message:

Form ready to submit

Tab Switcher

Classic UI pattern. One activeTab variable controls which panel shows. Docs pages, settings panels, feature lists — all use this.

Click the tabs:

Features

PHPue + AlpineJS = modern web apps without the bloat.

Documentation

Guides, examples, and API references to build faster.

Support

Join the community. Get help, share ideas.

Heads Up: x-for Doesn't Work Here

AlpineJS's x-for uses template cloning. PHPue works with the real DOM. They don't play nice together.

Do this instead — use p-for:

<div p-for="$item in $items">
    <span x-data="{ liked: false }">
        {{ $item.name }}
        <button @click="liked = !liked">
            <span x-show="!liked">♡</span>
            <span x-show="liked">♥</span>
        </button>
    </span>
</div>

PHPue handles the loop server-side. AlpineJS handles the like button client-side. Best of both.

Don't do this:

<template x-for="item in items">
    <div>...</div>  <!-- Nope -->
</template>

If you really need x-for, grab the x-ssr extension for SSR and CSR sync.

Directives Cheatsheet

x-data

Creates reactive scope

x-data="{ open: false }"
@click

Runs on click

@click="open = !open"
x-text

Sets text content

x-text="message"
x-show

Toggles visibility

x-show="isVisible"
x-model

Two-way form binding

x-model="username"
:class

Dynamic CSS classes

:class="{ 'active': isActive }"
🏔️

You've Got the Foundations

Counters, forms, tabs — you can now add real interactivity to your PHPue apps.

Covered:

✓ Adding AlpineJS
✓ Live counters
✓ PHP + AlpineJS
✓ Reactive forms
✓ Tab switchers
✓ p-for vs x-for