【问题标题】:Laravel Blade custom directive? looped includesLaravel Blade 自定义指令?循环包括
【发布时间】:2017-10-08 14:23:23
【问题描述】:

假设我的模板中有以下代码

@foreach($page->getBannersForPosition('bottom') as $banner)
    @include('partials.'.$banner->type.'.'.$banner->layout)
@endforeach

但我想拥有(或类似的东西,简单快捷)

@banners('bottom')

我将如何在 Laravel / Blade 中执行此操作? 我在文档中找到了关于自定义指令的部分,但它并没有真正帮助我。

提前致谢!

【问题讨论】:

标签: laravel laravel-5 blade directive laravel-blade


【解决方案1】:

为什么不将 foreach 移到它自己的部分。然后你的代码可以读取

@include('banners.bottom')

然后在 banners.bottom.blade 里面你会有你的 foreach 循环

@foreach($page->getBannersForPosition('bottom') as $banner)
    @include('partials.'.$banner->type.'.'.$banner->layout)
@endforeach

【讨论】:

  • 这是个好主意。我将给包含位置的变量提供一个变量。
  • 我最终得到了@include('banners', ['bannerPosition' => 'bottom'])
猜你喜欢
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 2018-10-05
  • 2017-01-26
  • 1970-01-01
相关资源
最近更新 更多