问题
With Laravel Blade, is there an elegant way to check if a view exists before doing an @include
?
For example I'm currently doing this:
@if(View::exists('some-view'))
@include('some-view')
@endif
Which gets quite cumbersome when 'some-view' is a long string with variables inside.
Ideally I'm looking for something like this:
@includeifexists('some-view')
Or to make @include
just output an empty string if the view doesn't exist.
As an aside, I would also like to provide a set of views and the first one that exists is used, e.g.:
@includefirstthatexists(['first-view', 'second-view', 'third-view'])
And if none exist an empty string is output.
How would I go about doing this? Would I need to extend BladeCompiler or is there another way?
回答1:
Had a similar issue. Turns out that from Laravel 5.3 there is an @includeIf
blade directive for this purpose.
Simply do @includeIf('some-view')
来源:https://stackoverflow.com/questions/35198014/check-if-a-view-exists-and-do-an-include-in-laravel-blade