pustato/laravel-blade-render-flow

Laravel Blade渲染流程扩展

1.0.0 2017-04-11 09:41 UTC

This package is not auto-updated.

Last update: 2024-09-20 19:37:30 UTC


README

此包添加了几个blade指令,允许您重用模板的一部分。

安装

使用composer要求此包

composer require pustato/laravel-blade-render-flow

将ServiceProvider添加到您的config/app.php中的providers部分

Pustato\LaravelBladeRenderFlow\ServiceProvider::class,

使用方法

@capture

允许您渲染模板的一部分一次,并在以后重用它。

  • @capture(block_name)开始捕获名为block_name的模板块。
  • @endcapture结束块。
  • @flushcapture结束块并立即渲染它。
  • @flush(block_name)渲染存储的块。
  • @clearcapture([block_name])如果指定了block_name,则忘记此名称的块,否则 - 忘记所有存储的块。

示例

@php
    $src = 'http://placehold.it/100x100';
    $title = 'Placeholder image';
@endphp
@capture(placeholder)
<img src="{{ $src }}" title="{{ $title }}" alt="{{ $titel }}"/>
@endcapture

<div>
    @flush('placeholder')
</div>
<div>
    @flush('placeholder')
</div>
<div>
    @flush('placeholder')
</div>
@clearcapture('placeholder')

将渲染为

<div>
    <img src="http://placehold.it/100x100" title="Placeholder image" alt="Placeholder image"/>
</div>
<div>
    <img src="http://placehold.it/100x100" title="Placeholder image" alt="Placeholder image"/>
</div>
<div>
    <img src="http://placehold.it/100x100" title="Placeholder image" alt="Placeholder image"/>
</div>

@template

允许您指定子模板的某个部分并将其存储在内存中。

  • @template(name)开始带有name的子模板。
  • @endtemplate结束子模板。
  • @render(name[, params])渲染具有指定参数的子模板(类似于@include)。

示例

@template('img')
<img src="{{ $src or 'http://placehold.it/100x100' }}" alt="{{ $title or 'Image alt' }}" title="{{ $title or 'Image title' }}"/>
@endtemplate

<div>
    @render('img')
</div>
<div>
    @render('img', ['src' => 'http://placehold.it/350x350', 'title' => 'Large placeholder'])
</div>
<div>
    @render('img', ['src' => 'http://placehold.it/32x32', 'title' => 'Tiny placeholder'])
</div>

结果

<div>
    <img src="http://placehold.it/100x100" alt="Image alt" title="Image title">
</div>
<div>
    <img src="http://placehold.it/350x350" alt="Large placeholder" title="Large placeholder">
</div>
<div>
    <img src="http://placehold.it/32x32" alt="Tiny placeholder" title="Tiny placeholder">
</div>