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)

4
  • 1
    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.
    – Avalanche
    Commented Aug 19, 2015 at 18:05
  • I am just wondering why would you include something that does not exist or what would be the scenario you are trying to solve?
    – mdamia
    Commented Aug 19, 2015 at 18:09
  • @mdamia : 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.
    – Cedric
    Commented Aug 20, 2015 at 7:49
  • 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).
    – Cedric
    Commented Aug 21, 2015 at 9:35

5 Answers 5

43

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

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

1
  • This should be the accepted answer.
    – TVBZ
    Commented Feb 4, 2022 at 13:19
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

1
  • This is correct. In your first example, you missed a ' at the end of the include parameter.
    – Maurice
    Commented Jan 10, 2017 at 15:55
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')) {
    \\
}
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.

1

Laravel >= 7, there directive includeIf

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.