Skip to content

A Laravel Livewire integration for Statamics antlers engine

Notifications You must be signed in to change notification settings

MrMooky/statamic-livewire

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Statamic Livewire

Statamic 3.0+ Latest Version on Packagist

A third-party Laravel Livewire integration for Statamic.

It's as easy as it get's to get stared with Livewire if using Statamic 3.

Installation

Pull in the Livewire package with composer

composer require jonassiewertsen/statamic-livewire

General documentation

Laravel Livewire Docs

Setup inside your template

Include the Livewire styles and scripts on every page that will be using Livewire:

...
    <!-- If using Antlers -->
    {{ livewire:styles }}

    <!-- If using Blade -->
    @livewireStyles
</head>
<body>

    ...
    <!-- If using Antlers -->
    {{ livewire:scripts }}

    <!-- Blade -->
    @livewireScripts
</body>
</html>

Include components

You can create Livewire components as described in the general documentation. To include your Livewire component:

<body>
    <!-- If using Antlers -->
    {{ livewire:your-component-name }}
    
    <!-- If using Blade -->
    <livewire:your-component-name />
</body>

Blade or Antlers? Both!

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');
    }
}

More Information: (https://laravel-livewire.com/docs/quickstart#create-a-component)

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 wathever you like. No need to change anything inside your component Controller. How cool is that?

Passing Initial Parameters

You can pass data into a component by passing additional parameters

<!-- If using Antlers -->
{{ livewire:your-component-name :contact="contact" }}

<!-- If using Blade -->
<livewire:your-component-name :contact="$contact">

To intercept with those parameters, mount them and store the data as public properties.

use Livewire\Component;

class ShowContact extends Component
{
    public $name;
    public $email;

    public function mount($contact)
    {
        $this->name = $contact->name;
        $this->email = $contact->email;
    }

    ...
}

The Official Livewire documentation

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 similar. Make sure to use the Jonassiewertsen\Livewire\WithPagination namespace for your trait if working with Antlers.

In your Livewire component view:

{{ entries }}
    ...
{{ /entries }}

{{ links }}
use Jonassiewertsen\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());
    }
}

Entangle: Sharing State Between Livewire And Alpine

In case you want to share state between Livewire and Alpine, there is a Blade directive called @entangle: Livewire docs

To be usable with Antlers, we do provide an dedicated Tag:

<!-- With Antlers -->
<div x-data="{ open: {{ livewire:entangle property='showDropdown' }} }">
        
<!-- With Blade -->
<div x-data="{ open: @entangle('showDropdown').defer }">

This: Accessing the Livewire component

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

<!-- With Antlers -->
{{ livewire:this set="('name', 'Jack')" }}

<!-- With Blade -->
@this.set('name', 'Jack')

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:

Requirements

  • PHP 7.4 or 8.0
  • Laravel 7 or 8
  • Statamic 3

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.

About

A Laravel Livewire integration for Statamics antlers engine

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%