Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel blade @include view using variable

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.

like image 505
Sibtain Norain Avatar asked Jan 15 '15 12:01

Sibtain Norain


1 Answers

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
like image 65
bdtiger Avatar answered Nov 04 '22 23:11

bdtiger