Check if a view exists and do an @include in Laravel Blade

本秂侑毒 提交于 2019-12-09 09:46:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!