Luuk Skeur
Luuk Skeur

Reputation: 1940

include nested views if exists in blade, laravel 5

I am trying to include another view if it exists. I have a main page.blade.php which should render the content of the selected page. in the back-end of the application I get a flat array with all the content in it (id and name). So now if a template exists I want to render it but first I need to check if the template actually exists.

I got the following code right now:

@foreach($flattenPage as $page)
    @if(file_exists('templates.'.strtolower($page['name'])))
        @include('templates.'.strtolower($page["name"]))
    @endif
@endforeach

The problem is that it won't get through the if statement. I also tried:

@if(file_exists('../templates/'.strtolower($page['name']).'.blade.php'))

The current template is views/page/show.blade.php and I need to render views/templates/myContent.blade.php

Upvotes: 3

Views: 2632

Answers (2)

Raghavendra N
Raghavendra N

Reputation: 3717

From Laravel 5.2 you can also do this:

@includeIf('view.name', ['some' => 'data'])

Your code will look like this:

@foreach($flattenPage as $page)
    @includeIf('templates.'.strtolower($page["name"]))
@endforeach

Upvotes: 1

Laurence
Laurence

Reputation: 60048

If you need to determine if a view exists - you can use exists method (as described in the docs)

@if (view()->exists('templates.'.strtolower($page["name"])))
    @include('templates.'.strtolower($page["name"]))
@endif

Upvotes: 11

Related Questions