A Laravel Livewire integration for Statamics antlers engine

Overview

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.

Comments
  • Make errors work in Antlers

    Make errors work in Antlers

    There is currently no way to get validation errors with Antlers. With Blade you can easily do the following:

    @if ($errors->any())     
        <p>There were $errors->count() errors</p> 
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    @endif
    

    I'm currently using a custom tag to get around this. But I was wondering if you are open to a PR that adds the functionality to this package. It could work like this:

    {{ if {livewire:errors} }}
        <p>There were {{ livewire:error_count }} errors</p> 
        <ul>
            {{ livewire:errors }}
                <li>{{ error }}</li>
            {{ livewire:errors }}
        </ul>
    {{ /if }}
    
    enhancement 
    opened by aerni 10
  • How to use pagination?

    How to use pagination?

    Is there a way to use pagination like documented here? In Blade you would use {{ $posts->links() }} to render the pagination view? I couldn't figure out a way to do this in Antlers.

    enhancement help wanted 
    opened by aerni 9
  • Add Statamic Context

    Add Statamic Context

    The idea of this PR is to make the Statamic context available as public property in your Livewire components. Having the context available can help to reduce the number of properties you have to pass down the component in your view.

    Simply add the WithContext trait to your component and you're good to go.

    I'm not completely sure, but one possible limitation is probably the reactivity of the context property. If you want to make certain context properties reactive, you'd still have to manually create a new livewire property and mount the context value to it. At least this would reduce the noise in your template.

    Another idea could be to do some magic and automatically add all context properties as a livewire property. But this might lead to other issues as well.

    Please give feedback on what you think about this idea.

    opened by aerni 7
  • Entangle in Antlers throws Invalid or unexpected token in console

    Entangle in Antlers throws Invalid or unexpected token in console

    Hey Jonas

    Thanks again for this package. I have discovered an issue:

    • Use antlers for a component view
    • Use Alpine to share state between Alpine and Livewire using @entangle see docs
    • See error in console when viewing the component

    I guess this due to the use of @ in the template and it breaks something in Antlers, although I'm not sharp enough to know exactly what.

    I have made a demo repo that you can clone to see the issue in real life: https://github.com/johncarter-/statamic-antlers-livewire-entangle-error

    Sorry I can't fix it.

    opened by johncarter- 6
  • Error when redirecting

    Error when redirecting

    Redirecting to another page within a Livewire component throws this error:

    Jonassiewertsen\Livewire\Tags\Livewire::wildcard(): Return value must be of type string, null returned
    

    You can reproduce this using this code:

    public function mount()
    {
        return redirect()->to('/');
    }
    

    Versions: Statamic 3.2.16 Pro Laravel 8.64.0 PHP 8.0.12 jonassiewertsen/statamic-livewire 2.7.0

    bug 
    opened by aerni 3
  • Breaking JS after emitting events

    Breaking JS after emitting events

    Hi,

    I'm running into a weird issue and I think it has something to do with statamic-livewire.

    What I try to do

    I use a slider. When the slider is updated (a new slide is selected) I want the data on the selected slide to be available in Livewire.

    What happens

    When I emit an event to Livewire, or update an entangled value, the JS of the slider breaks.

    What I tried

    • Trying different sliders (Swiper & Flickity)
    • Trying to use Alpine and Vanille JS
    • Trying to emit events using Antlers and Blade
    • Trying to use entangle or {{ livewire:this set="" }}

    My code

        <div x-data="{flkty: null}" x-init="
            flkty = new Flickity( '.main-carousel', {
                wrapAround: true
            });
    
            flkty.on('select', function(index) {
                {{ livewire:this set="('dataset', flkty.selectedElement.dataset.test)" }}
            });
        ">
    
            <div class="main-carousel">
              <div class="w-full" data-test="data 1">Slide 1</div>
              <div class="w-full" data-test="data 2">Slide 2</div>
              <div class="w-full" data-test="data 3">Slide 3</div>
            </div>
    
        </div>
    
        <div class="output">
            {{ dataset }}
        </div>
    

    Livewire:

    class MyComponent extends Component
    {
        
        public $dataset;
    
        public function render()
        {
            return view('livewire.my-component');
        }
    }
    
    wontfix 
    opened by sytheveenje 2
  • Mounted parameters get augmented

    Mounted parameters get augmented

    Is this the desired effect that when passing an Statamic\Entries\Entry parameter it gets augmented?

    On vendor/jonassiewertsen/statamic-livewire/src/Tags/Livewire.php: $parameters->toArray() will convert the Entry to an augmented array. If we replace ->toArray() with ->all() the Entry stays an object.

    image

    Here is the output from Ray from line #43:

    image

    Let me know if it's a bug and you want me to PR.

    needs more information 
    opened by FR6 2
  • Nested components getting re-rendered

    Nested components getting re-rendered

    I'm using this add on to build a checkout component, which in turn has some nested components (basket view and country/state selector).

    The issue is that when the nested component (eg country/state) emits an event that affects the parent component (checkout), the child components get re-rendered. If I rebuild the components in blade rather than antlers this does not happen.

    Is this a known issue? Is there a work around?

    wontfix 
    opened by ryanmitchell 2
  • Pagination when using a filter

    Pagination when using a filter

    Hi

    I need to perform a filter before using the pagination. But when i do got an error: Method Statamic\Entries\EntryCollection::paginate does not exist.

    My code:

        {
                $entries = Entry::query()
                ->where('collection', 'projects')
                ->where('locale', Site::current()->handle)
                ->get()
                ->filter(function ($entry, $key) {
                    return $this->filters['categories'] ? (array_intersect($entry->get('categories'), $this->filters['categories'])) : true;
                    }
                )
                ->filter(function ($entry, $key) {
                    return $this->filters['q'] ?  str_contains($entry->get('title'), $this->filters['q']) : true;
            }
            )->paginate(3);
    
    
            return $this->withPagination('entries', $entries);
        }
    

    This works:

    protected function entries()
        {
                $entries = Entry::query()
                ->where('collection', 'projects')
                ->where('locale', Site::current()->handle)
                ->paginate(3);
    
    
            return $this->withPagination('entries', $entries);
        }
    
    

    But i need to filter the content beforehand. How can i do this?

    opened by freshface 1
  • Pagination {{ updated_at }} variable changing format on livewire update

    Pagination {{ updated_at }} variable changing format on livewire update

    Hi, not sure If I'm missing something, or if this is something that can be easily corrected with a changed approach... but wanted to raise it in case it's something that can be resolved in the addon.

    I'm using the statamic-livewire addon for a simple Articles/news feed, using the use Jonassiewertsen\Livewire\WithPagination namespace.

    When using the pagination, the Statamic {{ updated_at }} variable is changing from 'March 11th, 2022' to: '2022-03-11 06:00:27'.

    Anything simple I can do to resolve this? I'm using Antlers for the view, so ideally the solution would be Antlers rather than blade.

    opened by andrew-ireland 1
  • Global Statamic Variables

    Global Statamic Variables

    Using global Statamic variables with Antlers would be very useful, but since livewire is not persisting private variables through render cycles and setting the global set to a public variable on mount will produce an error I'm not quite sure what the best practice for this would be.

    This approach works, but doesn't feel right to me:

    public function mount($fallbacks) {
            session(['fallbacks' => $fallbacks]);
    }
    
    public function render()
    {
        return view('blocks.counter', [
          "fallbacks" => session('fallbacks'),
        ]);
    }
    

    An this is how the component is getting called:

    {{ livewire:counter :fallbacks="fallbacks" }}
    

    Would be awesome do add some best practice to the readme.

    Thanks,

    opened by valschr 1
  • model.defer in antler templates

    model.defer in antler templates

    Hi!

    I’m trying to bind a deferred model to an input in an antlers template like that:

    <input type="text" wire:model.defer="searchTerm" />
    

    Unfortunately this doesn’t work at all. Using it without defer runs like expected.

    Does someone has a suggestion what might be wrong?

    Thank you :)

    bug 
    opened by mkriegeskorte 0
Releases(v2.9.0)
Owner
Jonas Siewertsen
Jonas Siewertsen
A TALL (Tailwind CSS, Alpine.js, Laravel and Livewire) Preset for Laravel

Laravel TALL Preset A front-end preset for Laravel to scaffold an application using the TALL stack, jumpstarting your application's development. If yo

Laravel Frontend Presets 1.8k Jan 7, 2023
Use Laravel's Blade templating engine outside of Laravel.

Use Laravel's Blade templating engine outside of Laravel. This package provides a standalone version of Laravel's Blade templating engine for use outs

Ryan Chandler 22 Jan 2, 2023
Laravel Livewire Excel Upload with Progressbar

Laravel Livewire Excel Upload with Progressbar This is sample project, that explains how to import Excel files with progress bar Steps to run project

Prabakaran T 5 Oct 6, 2022
Belich Tables: a datatable package for Laravel Livewire

Belich Tables is a Laravel package base on Livewire and AlpineJS that allows you to create scaffold datatables with search, column sort, filters, pagination, etc...

Damián Aguilar 11 Aug 26, 2022
Laravel Livewire full page component routing.

Laravel Livewire Routes Laravel Livewire full page component routing. This package allows you to specify routes directly inside your full page Livewir

null 22 Oct 6, 2022
A laravel Livewire Dynamic Selects with multiple selects depending on each other values, with infinite levels and totally configurable.

Livewire Combobox: A dynamic selects for Laravel Livewire A Laravel Livewire multiple selects depending on each other values, with infinite levels of

Damián Aguilar 25 Oct 30, 2022
Dynamic Laravel Livewire Bootstrap 5 modals.

Laravel Livewire Modals Dynamic Laravel Livewire Bootstrap 5 modals. Requirements Bootstrap 5 Installation Require the package: composer require basti

null 55 Dec 27, 2022
Laravel Livewire form component with declarative Bootstrap 5 fields and buttons.

Laravel Livewire Forms Laravel Livewire form component with declarative Bootstrap 5 fields and buttons. Requirements Bootstrap 5 Installation composer

null 49 Oct 29, 2022
Auto generate routes for Laravel Livewire components

livewire-auto-routes Auto generate routes for Laravel Livewire Components. Requirements Livewire 2 Laravel 8 php 8 Installation composer require tanth

Tina Hammar 19 May 11, 2022
Laravel Livewire UI, Auth, & CRUD starter kit.

Laravel Livewire Ui This package provides Laravel Livewire & Bootstrap UI, Auth, & CRUD scaffolding commands to make your development speeds blazing f

null 97 Nov 15, 2022
Laravel 8 + CoreUI + Livewire + Datatables (CRUD)

Laravel 8 + CoreUI + Livewire + Datatables About Laravel 8 + CoreUI + Livewire Datatables Whats Inside Laravel Core UI - (https://github.com/HZ-HBO-IC

Muhammad Rheza Alfin 38 Nov 3, 2022
Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.

About LivewireUI Spotlight LivewireUI Spotlight is a Livewire component that provides Spotlight/Alfred-like functionality to your Laravel application.

Livewire UI 792 Jan 3, 2023
Dynamic Laravel Livewire Bootstrap toasts.

Laravel Livewire Toasts This package allows you to dynamically show Bootstrap toasts via Laravel Livewire components. Documentation Requirements Insta

null 13 Nov 12, 2022
Laravel UI, Auth, & CRUD scaffolding package using Bootstrap & Livewire.

bastinald/ux Laravel UI, Auth, & CRUD scaffolding package using Bootstrap & Livewire. Features Automatic migrations Automatic routing Automatic passwo

null 33 Nov 26, 2022
Laravel package integrating PHP Flasher into Livewire applications

A powerful and flexible flash notifications system for PHP, Laravel, Symfony ?? PHP Flasher helps you to add flash notifications to your PHP projects.

PHP Flasher 9 Jul 5, 2022
Laravel Livewire (TALL-stack) form generator with realtime validation, file uploads, array fields, blade form input components and more.

TALL-stack form generator Laravel Livewire, Tailwind forms with auto-generated views. Support Contributions Features This is not an admin panel genera

TinaH 622 Jan 2, 2023
Laravel Livewire package for clone screen with the help of websocket

screen-wire This is a Laravel Livewire package. It allow you to see what users are doing on their screen. Installation composer require mrbohem/scree

Shivam Upadhyay 2 Aug 29, 2022
A Laravel Code Generator based on your Models using Blade Template Engine

Laravel Code Generator is a PHP Laravel Package that uses Blade template engine to generate code for you. The difference between other code generators

Victor Yoalli 59 Dec 5, 2022
A TALL-based Laravel Livewire component to replace the (multiple) select HTML input form with beautiful cards.

TALL multiselect cards A TALL-based Laravel Livewire component to replace the (multiple) select HTML input form with beautiful cards. Table of content

Frederic Habich 19 Dec 14, 2022