Reputation: 679
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.
Upvotes: 7
Views: 11464
Reputation: 1
You can use PHP code normally inside the @include. For example, if you want to concatenate a string and a variable simply do this:
@include('string'.$var)
If you can not, maybe you need to download the proper extension in your code editor. In VS Code, Laravel Blade Snippets.
Upvotes: -1
Reputation: 521
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.
@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
@foreach (Config::get('constants.tiles') as $tile)
@if (Session::get('currentUser')->get('permissions')[$tile]['read'] == 1)
@include('dashboard.tiles.' . $tile)
@endif
@endforeach
Upvotes: 19