检查视图是否存在,并在Laravel Blade中执行@include [英] Check if a view exists and do an @include in Laravel Blade
问题描述
使用Laravel Blade,是否有一种优雅的方法在执行@include
之前检查视图是否存在?
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
当"some-view"是一个包含变量的长字符串时,这会变得很麻烦.
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')
或者使@include
仅在视图不存在时输出一个空字符串.
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.
我将如何去做?我需要扩展BladeCompiler还是还有另一种方法?
How would I go about doing this? Would I need to extend BladeCompiler or is there another way?
推荐答案
出现了类似的问题.事实证明,从Laravel 5.3开始,有一个@includeIf
刀片指令用于此目的.
Had a similar issue. Turns out that from Laravel 5.3 there is an @includeIf
blade directive for this purpose.
只需执行@includeIf('some-view')
这篇关于检查视图是否存在,并在Laravel Blade中执行@include的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!