marcorieser/statamic-livewire
Composer 安装命令:
composer require marcorieser/statamic-livewire
包简介
A Laravel Livewire integration for Statamic.
README 文档
README
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
- Installation
- Upgrade
- Livewire documentation
- Features
- Blade or Antlers? Yes, please!
- Include components
- Passing Initial Parameters
- Keying Components
- Manually including Livewire's frontend assets
- Manually bundling Livewire and Alpine
- Static caching
@scriptand@assets- Computed Properties
- Cascade
- Multi-Site / Localization
- Lazy Components
- Paginating Data
- Synthesizers
- Entangle: Sharing State Between Livewire And Alpine
- This: Accessing the Livewire component
- Other Statamic Livewire Packages
- Credits
- Support
- License
Requirements
- PHP 8.2+
- Laravel 11+
- 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:csrf.replaced', () => Livewire.start()); } else { Livewire.start(); }
@script and @assets
Antlers versions of @script and @assets are provided:
<body> {{ livewire:script }} <script>console.log('hello')</script> {{ /livewire:script }} </body>
<body> {{ livewire:assets }} <script src="some-javascript-library.js"></script> {{ /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 }}
Cascade
Normally all the variables in the Cascade are only available on initial render and get lost between Livewire requests. This means you'd need pass in the required ones into the component yourself.
To make our lives a bit easier, you can add the #[Cascade] attribute to your component.
This is only needed for Antlers views and mirrors the logic of Blade's @cascade directive.
use Livewire\Component; use MarcoRieser\Livewire\Attributes\Cascade; #[Cascade] class ShowArticle extends Component { ... }
Now you can access the variables from the Cascade directly in your Antlers view, even on subsequent renders:
<h1>{{ title }}</h1> {{ seo_title }}
You can also limit which cascade keys are exposed (and provide defaults):
#[Cascade([
'title',
'seo_title' => 'Fallback title',
])]
class ShowArticle extends Component {}
For subsequent requests, the addon restores the Cascade using the original Livewire URL, so site, request, and content data resolve as expected.
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.
- Run
php artisan vendor:publish - Select
statamic-livewirein the list - 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.
<script> document.addEventListener('livewire:initialized', function () { // `{{ livewire:this }}` returns the instance of the current component {{ livewire:this }}.set('name', 'Jack') }) </script>
You can access and perform actions on the Livewire component like this:
<script> document.addEventListener('livewire:initialized', function () { // With Antlers {{ livewire:this set="('name', 'Jack')" }} // With Blade @this.set('name', 'Jack') }) </script>
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.
marcorieser/statamic-livewire 适用场景与选型建议
marcorieser/statamic-livewire 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 112.36k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2025 年 01 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「addon」 「laravel」 「statamic」 「livewire」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 marcorieser/statamic-livewire 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marcorieser/statamic-livewire 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 marcorieser/statamic-livewire 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Statamic Fine Seo addon
This composer plugin enables installation of GravityForms WordPress plugin and its addons.
A lightweight floating toolbar for Statamic that gives logged-in users quick access to the CP and entry editor
Dropzone field for Laravel Backpack
This ConcreteCMS/concrete5 addon installs a block type that when added to a page allows you to redirect visitors to this page to another page that you specify or an external URL.
统计信息
- 总下载量: 112.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 23
- 点击次数: 12
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-27