stephancasas/blade-wants-attributes 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stephancasas/blade-wants-attributes

最新稳定版本:1.0.1

Composer 安装命令:

composer require stephancasas/blade-wants-attributes

包简介

Provide all attributes (including irregular patterns) to Laravel Blade class components.

README 文档

README

Latest Version on Packagist Total Downloads

blade-wants-attributes offers you the ability to use Blade/HTML-defined attributes within the constructors of Laravel Blade class components.

Why?

Blade class components have many advantages, but require developers to declare attributes as constructor arguments if they wish to use them in the class signature. Because of this requirement, attributes containing non-standard characters (e.g. wire:model, wire:model.defer, etc.) are completely inaccessible to the class constructor or methods called by the class constructor.

blade-wants-attributes registers a Blade pre-compiler which provides attributes to the ViewServiceProvider just prior to initialization of a component instance. Attributes are then made accessible to your component's constructor by adding the WantsAttributes trait to the class signature and calling $this->wantsAttributes() in the constructor.

Okay, but why?

It can be useful — particularly if you're writing a components package — to extract complex component logic into class methods. For example, you may — at times — wish to dynamically/conditionally render markup for Livewire or AlpineJS. To do this, you'll need to access non-standard HTML attributes which are unavailable to Blade class components.

Installation

You can install the package via composer:

composer require stephancasas/blade-wants-attributes

Usage

Apply the StephanCasas\BladeWantsAttributes\Traits\WantsAttributes trait to your class component, and call $this->wantsAttributes().

namespace App\View\Components;

use Illuminate\View\Component;

class Select extends Component
{
    use StephanCasas\BladeWantsAttributes\Traits\WantsAttributes;

    public $wireModel;

    public function __construct()
    {
        $this->wantsAttributes();

        $this->wireModel = $this->attributes
            ->get('wire:model');
    }

    //...

    public function render()
    {
        return view('components.select');
    }
}

Alternatively, if you do not wish to apply the attribute bag to your component's $attributes property, you can also access the provided attributes via the $this->getAllAttributes() method:

namespace App\View\Components;

use Illuminate\View\Component;

class Select extends Component
{
    use StephanCasas\BladeWantsAttributes\Traits\WantsAttributes;

    public $isLivewire;

    public function __construct()
    {
        $this->isLivewire = $this->getAllAttributes()
            ->has('wire:model');
    }

    //...

    public function render()
    {
        return view('components.select');
    }
}

License

MIT — see License File for more information.

统计信息

  • 总下载量: 11
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固