laravellaravel-4laravel-blade

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.


Solution

  • 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