How to include a blade template only if it exists?
Asked Answered
S

5

19

How to @include a blade template only if it exists ? I could do something like that :

@if (File::exists('great/path/to/blade/template.blade.php'))
   @include('path.to.blade.template')
@endif

But that really isn't elegant and efficient.

I could include it without if statements, and catch & hide errors if the file isn't here, but that is a little dirty, if not barbaric.

What would be great is something like that :

@includeifexists('path.to.blade.template')

(pseudo code, but this blade command does not exist)

Shovelhead answered 19/8, 2015 at 17:48 Comment(4)
You could surely extend Blade by adding a custom function to hide the if statements, but I suppose it is only worthy if you have plenty of these cases to use it at.Blackmarketeer
I am just wondering why would you include something that does not exist or what would be the scenario you are trying to solve?Koralle
@Koralle : I have hundreds of page, with the same structure. Some have partials files. I include partial only for pages who need it. I could use two template : one who an inclusion directive for the pages who need it, and one without. But the logic is simpler in the first way.Shovelhead
Warning : Only use the described thing if you absolutely need it. Two cons about it : first, as it is not a standard approach, your colleagues will get confused in the first place - this doesn't comply with KISS principles. Second, I guess it will result in slightly slower pages, as PHP will have to check if a file exist to include it (I guess).Shovelhead
K
31

You can use View::exists() to check if a view exists or not.

@if(View::exists('path.to.view'))
    @include('path.to.view')
@endif

Or you can extend blade and add new directive

Blade::directive('includeIfExists', function($view) {

});

Check out the official document here: http://laravel.com/docs/5.1/blade#extending-blade

Khalif answered 19/8, 2015 at 18:5 Comment(1)
This is correct. In your first example, you missed a ' at the end of the include parameter.Officious
D
43

Had a similar issue. Turns out there is an @includeIf blade directive for this purpose.

Simply do @includeIf('path.to.blade.template')

Delaryd answered 29/5, 2016 at 20:14 Comment(1)
This should be the accepted answer.Boner
K
31

You can use View::exists() to check if a view exists or not.

@if(View::exists('path.to.view'))
    @include('path.to.view')
@endif

Or you can extend blade and add new directive

Blade::directive('includeIfExists', function($view) {

});

Check out the official document here: http://laravel.com/docs/5.1/blade#extending-blade

Khalif answered 19/8, 2015 at 18:5 Comment(1)
This is correct. In your first example, you missed a ' at the end of the include parameter.Officious
F
3

When needed in a Controller (from this documentation):

Determining If A View Exists If you need to determine if a view exists, you may use the exists method after calling the view helper with no arguments. This method will return true if the view exists on disk:

use Illuminate\Support\Facades\View;

if (View::exists('emails.customer')) {
    \\
}
Forth answered 21/2, 2018 at 8:18 Comment(0)
N
3

Late to the show, but there's another quite handy template function:

Often you'd like to include a template when it is present, or include some other template if not. This could result in quite ugly if-then constructs, even when you use '@includeIf'. You can alse do this:

@includeFirst([$variableTemplateName, "default"]);

This will include - as the function name suggests - the first template found.

Nisan answered 26/10, 2020 at 11:34 Comment(0)
S
1

Laravel >= 7, there directive includeIf

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

https://laravel.com/docs/7.x/blade#including-subviews

Specification answered 30/9, 2020 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.