Livewire Icon

Livewire

Livewire Main Screenshot

A third-party Laravel Livewire integration for Statamic. It aims to make it as easy as possible to use Livewire in Statamic.

Table of Contents

Requirements

  • PHP 8.2
  • Laravel 11, 12
  • Statamic 5

Installation

Install the addon via composer:

composer require marcorieser/statamic-livewire

Upgrade

Below is a list with specific upgrade instructions.

Livewire documentation

In general, all Livewire specific information can be found in the official Livewire Docs.

Features

Blade or Antlers? Yes, please!

If creating a Livewire component, you need to render a template file

namespace App\Http\Livewire;
 
use Livewire\Component;
 
class Counter extends Component
{
public function render()
{
return view('livewire.counter');
}
}

Normally your template file would be a blade file, named counter.blade.php. Great, but what about Antlers? Rename your template to counter.antlers.html, use Antlers syntax and do whatever you like. No need to change anything inside your component Controller. How cool is that?

More Information: (https://livewire.laravel.com/docs/components)

Include components

You can create Livewire components as described in the general documentation. To include your Livewire component in Antlers, you can use the livewire tag:

{{ livewire:your-component-name }}

If you want to include a component from a dynamic variable, you can use the livewire:component tag:

{{ livewire:component :name="variable" }}

Passing Initial Parameters

You can pass data into a component by passing additional parameters:

{{ livewire:your-component-name :contact="contact" }}

The Official Livewire documentation provides more information.

Keying Components

Livewire components are automatically keyed by default. If you want to manually key a component, you can use the key attribute.

{{ contacts }}
{{ livewire:your-component-name :key="id" }}
{{ /contacts }}

The Official Livewire documentation provides more information.

Manually including Livewire's frontend assets

By default, Livewire injects the JavaScript and CSS assets it needs into each page that includes a Livewire component. If you want more control over this behavior, you can manually include the assets on a page using the following Antlers tags:

<html>
<head>
{{ livewire:styles }}
</head>
<body>
 
{{ livewire:scripts }}
</body>
</html>

Manually bundling Livewire and Alpine

If you need to include some custom Alpine plugins, you need to manually bundle the Livewire and Alpine assets and disable the automatic injection by using the following Antlers tag. Remember to include the Livewire styles as well.

<html>
<head>
{{ livewire:styles }}
</head>
<body>
 
{{ livewire:scriptConfig }}
</body>
</html>

Static caching

This addon adds an AssetsReplacer class to make Livewire compatible with half and full static caching. You may customize the replacers in the config of this addon:

'replacers' => [
\MarcoRieser\Livewire\Replacers\AssetsReplacer::class,
],

If you are using full measure static caching, and you're manually bundling Livewire and Alpine as per the instructions above, you need to make sure to only start Livewire once the CSRF token has been replaced.

if (window.livewireScriptConfig?.csrf === 'STATAMIC_CSRF_TOKEN') {
document.addEventListener('statamic:nocache.replaced', () => Livewire.start());
} else {
Livewire.start();
}

@script and @assets

Antlers versions of @script and @assets are provided:

<body>
{{ livewire:script }}
 
{{ /livewire:script }}
</body>
<body>
{{ livewire:assets }}
 
{{ /livewire:assets }}
</body>

Computed Properties

When using Antlers, the computed properties are loaded automatically and only resolve when accessed. Simply access them as you would access a regular variable in the cascade. Read more about Computed Properties in the Livewire Docs.

#[Computed]
public function entries() {
return Entry::all();
}
{{ entries }}
{{ title }}
{{ /entries }}

Multi-Site / Localization

By default, your current site is persisted between Livewire requests automatically.
In case you want to implement your own logic, you can disable localization in your published config/statamic-livewire.php config.

Lazy Components

Livewire allows you to lazy load components that would otherwise slow down the initial page load. For this you can simply pass lazy="true" as argument to your component tag.

{{ livewire:your-component-name :contact="contact" lazy="true" }}

Paginating Data

You can paginate results by using the WithPagination trait.

Blade

To use pagination with Blade, please use the Livewire\WithPagination namespace for your trait as described in the Livewire docs.

Antlers

Pagination with Antlers does work similarly. Make sure to use the MarcoRieser\Livewire\WithPagination namespace for your trait if working with Antlers.

In your Livewire component view:

{{ entries }}
...
{{ /entries }}
 
{{ links }}
use MarcoRieser\Livewire\WithPagination;
 
class ShowArticles extends Component
{
use WithPagination;
 
protected function entries()
{
$entries = Entry::query()
->where('collection', 'articles')
->paginate(3);
 
return $this->withPagination('entries', $entries);
}
 
public function render()
{
return view('livewire.blog-entries', $this->entries());
}
}

Synthesizers

You can use the built-in Synthesizers to make your Livewire components aware of Statamic specific data types.

use Statamic\Entries\Entry;
 
class Foo extends Component
{
public Entry $entries;
}

Currently, the following types are supported:

  • Statamic\Entries\EntryCollection;
  • Statamic\Entries\Entry;
  • Statamic\Fields\Field;
  • Statamic\Fields\Fieldtype;
  • Statamic\Fields\Value;

To make it work, you need to enable that feature first.

  1. Run php artisan vendor:publish
  2. Select statamic-livewire in the list
  3. Enable synthesizers

Augmentation

By default, the Synthesizers augment the data before it gets passed into the antlers view. You can disable this by setting synthesizers.augmentation to false in your published config/statamic-livewire.php config.

Entangle: Sharing State Between Livewire And Alpine

It's worth mentioning that, since Livewire v3 now builds on top of Alpine, the @entangle directive is not documented anymore. Instead, it's possible to entangle the data via the $wire object.

<div x-data="{ open: $wire.entangle('showDropdown', true) }">

In case you want to share state between Livewire and Alpine, there is a Blade directive called @entangle. To be usable with Antlers, the addon provides a dedicated tag:

<div x-data="{ open: {{ livewire:entangle property='showDropdown' modifier='live' }} }">

This: Accessing the Livewire component

It's worth mentioning that, since Livewire v3 now builds on top of Alpine, the @this directive is not used widely anymore. Instead, it's possible to access and manipulate the state directly via JavaScript / the $wire object.

 

You can access and perform actions on the Livewire component like this:

 

Other Statamic Livewire Packages

If using Livewire, those packages might be interesting for you as well:

Did I miss a link? Let me know!

Credits

Thanks to:

  • Jonas Siewertsen for building the addon and give me the permission to take it over
  • Caleb and the community for building Livewire
  • Austenc for the Statamic marketplace preview image

Support

I love to share with the community. Nevertheless, it does take a lot of work, time and effort.

Sponsor me on GitHub to support my work and the support for this addon.

License

This plugin is published under the MIT license. Feel free to use it and remember to spread love.