Laravel Blade @include 视图使用变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27963037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel blade @include view using variable
提问by Sibtain Norain
I have a few blade template files which I want to include in my view dynamically based on the permissions of current user stored in session. Below is the code I've written:
我有一些刀片模板文件,我想根据存储在会话中的当前用户的权限动态包含在我的视图中。下面是我写的代码:
@foreach (Config::get('constants.tiles') as $tile)
@if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
@include('dashboard.tiles.' . $tile)
@endif
@endforeach
Blade is not allowing me to concatenate the constant string with the value of variable $tile. But I want to achieve this functionality. Any help on this would be highly appreciated.
Blade 不允许我将常量字符串与变量 $tile 的值连接起来。但是我想实现这个功能。对此的任何帮助将不胜感激。
回答by bdtiger
You can not concatenate string inside blade template command. So you can do assigning the included file name into a php variable and then pass it to blade template command.
您不能在刀片模板命令中连接字符串。因此,您可以将包含的文件名分配给 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- the dynamic includes with string concatenation works in blade templates
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