【问题标题】:Laravel blade @include view using variableLaravel 刀片 @include 使用变量的视图
【发布时间】:2015-03-13 19:44:57
【问题描述】:

我有一些刀片模板文件,我想根据会话中存储的当前用户的权限动态地将它们包含在我的视图中。下面是我写的代码:

@foreach (Config::get('constants.tiles') as $tile)
    @if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
        @include('dashboard.tiles.' . $tile)
    @endif
@endforeach

Blade 不允许我将常量字符串与变量 $tile 的值连接起来。但我想实现这个功能。对此的任何帮助将不胜感激。

【问题讨论】:

  • echo $tile 并确保它在那里。如果其他事情没问题,它应该可以正常工作。
  • 试试[{$tile}] - 解决了吗?
  • 注意:@include('dashboard.tiles.' . $tile) 适用于 Laravel 5.4

标签: laravel laravel-4 blade


【解决方案1】:

您不能在刀片模板命令中连接字符串。因此,您可以将包含的文件名分配给 php 变量,然后将其传递给刀片模板命令。

@foreach (Config::get('constants.tiles') as $tile)
   @if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
     <?php $file_name = 'dashboard.tiles.' . $tile; ?>
     @include($file_name)
   @endif
@endforeach

Laravel 5.4 - 动态包含字符串连接在刀片模板中工作

@foreach (Config::get('constants.tiles') as $tile)
   @if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
     @include('dashboard.tiles.' . $tile)
   @endif
@endforeach

【讨论】:

  • 是的,我做了同样的事情,但在 Controller 中,然后将视图名称作为数组发送到视图中。
猜你喜欢
  • 2016-09-18
  • 2018-03-09
  • 2014-03-15
  • 2017-08-24
  • 2018-06-18
  • 2018-11-07
  • 1970-01-01
  • 2017-02-08
  • 2014-02-23
相关资源
最近更新 更多