chstudio/laravel-transclude

此包已被弃用,不再维护。未建议替代包。

允许在 Blade 模板中使用 Angular transclude 功能。

v1.1.0 2017-08-30 16:43 UTC

This package is auto-updated.

Last update: 2022-10-06 04:48:03 UTC


README

Build Status Coverage Status

此包允许在 Blade 模板引擎中使用 transclusion。当您希望将视图作为组件处理时非常有用。它受到 Angular transclude 逻辑的启发。

安装

Latest Stable Version Total Downloads

可以使用 Composer 安装此项目。将以下内容添加到您的 composer.json

{
    "require": {
        "chstudio/laravel-transclude": "~2.0"
    }
}

或者运行此命令

composer require chstudio/laravel-transclude

更新 composer 后,将 ServiceProvider 添加到 config/app.php 文件中的 providers 数组。

Laravel 5.5+

此库与包自动发现功能兼容,因此您无需做任何事情... 如果您希望自行添加 providers,请遵循 官方文档指南

Laravel <5.5

将以下内容添加到 config/app.php 文件的 providers 部分

CHStudio\LaravelTransclude\TranscludeServiceProvider::class,

然后您可以在视图中使用新的 blade 指令!

用法

此包注册了三个新的 Blade 指令

  • @transclude / @endtransclude 用于在 transclude 块内编写
  • @transcluded 用于声明将要写入 transclusion 的空间

例如,考虑 Bootstrap 表单元素,它们都使用相同的全局结构。然后在该结构中,根据表单元素的不同,有不同的 html 块。

创建模板文件

input-group.blade.php

<div class="form-group">
    <label for="{{ $name }}" class="control-label">{{$label}}</label>

    @transcluded
</div>

radio.blade.php

@transclude('input-group')
    @foreach($options as $option)
    <div class="radio">
        <label>
            <input name="{{$name}}" type="radio" {{$option['value']==$selected?' checked':''}} value="{{$option['value']}}" />
            {{$option['label']}}
        </label>
    </div>
    @endforeach
@endtransclude

使用新块

然后编写这 3 个文件后,您可以使用 @include 指令添加元素

<form>
    @include('radio', [
        'options' => [
            ['value' => '1', 'label' => 'Option 1']
        ],
        'selected' => '1',
        'label' => 'My radio button'
        'name' => 'my-radio'
    ])
</form>

此代码将生成一个完整的单选元素,结合输入组和单选模板

<form>
    <div class="form-group">
        <label for="my-radio" class="control-label">My radio button</label>

        <div class="radio">
            <label>
                <input name="my-radio" type="radio" checked value="1" />
                Option 1
            </label>
        </div>
    </div>
</form>

贡献

我们欢迎每个人为此项目做出贡献。以下是一些您可以进行的贡献方式。