欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

控制器中模板中判断模板文件是否存在 laravel template is exists How to include a blade template only if it exists? 有大用

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)

shareimprove this question
 

2 Answers 正确答案

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

shareimprove this answer
 
   
This is correct. In your first example, you missed a ' at the end of the include parameter. – mauricehofman Jan 10 at 15:55

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

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

shareimprove this answer
 
1 
Please don't post duplicate answers: stackoverflow.com/a/37514492/3933332 If you think the two questions are duplicate please vote/flag so. Otherwise tweak your answers to answer the questions specifically. – Rizier123 May 29 '16 at 20:17
1 
Not my problem tbh. Also I did, please note the difference in included view name. – BARNZ May 30 '16 at 18:13
   
Amazing tip. Thanks. – user2094178 Dec 2 '16 at 22:40
1 
Rizier123, Had this guy not posted a duplicate answer, people like me wouldn't have found the right solution to their problem. The duplicate has far less views, see it for yourself – Alex Feb 15 at 22:17
1 
People like @Rizier123 are ruining StackOverflow. – StackOverflowed Apr 1 at 16:48

来自 https://stackoverflow.com/questions/32102272/how-to-include-a-blade-template-only-if-it-exists

 

I am trying to include another view if it exists. I have a main page.blade.php which should render the content of the selected page. in the back-end of the application I get a flat array with all the content in it (id and name). So now if a template exists I want to render it but first I need to check if the template actually exists.

I got the following code right now:

@foreach($flattenPage as $page)
    @if(file_exists('templates.'.strtolower($page['name'])))
        @include('templates.'.strtolower($page["name"]))
    @endif
@endforeach

The problem is that it won't get through the if statement. I also tried:

@if(file_exists('../templates/'.strtolower($page['name']).'.blade.php'))

The current template is views/page/show.blade.php and I need to render views/templates/myContent.blade.php

shareimprove this question
 
   
It's probably a relative path issue. Try file_exists(base_path('resources/views/templates/'.strtolowe‌​r($page['name']).'.b‌​lade.php')) – lukasgeiter Mar 31 '15 at 21:28

1 Answer 正确答案

 
 

If you need to determine if a view exists - you can use exists method (as described in the docs)

@if (view()->exists('templates.'.strtolower($page["name"])))
    @include('templates.'.strtolower($page["name"]))
@endif
shareimprove this answer
 
   
Thanks! this works for me – Luuk Skeur Apr 1 '15 at 12:57
   
@LuukvandeScheur - if that works, please dont forget to upvote and accept this answer. Thanks. – LaurenceApr 1 '15 at 13:02
   
That did the trick, this if. – mistery_girl Nov 28 '15 at 17:00

来自 https://stackoverflow.com/questions/29378637/include-nested-views-if-exists-in-blade-laravel-5

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

 

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?

shareimprove this question
 
   
I'm 95 % certain you need to write your own method for that, but luckily, it's easy: laravel.com/docs/5.1/blade#extending-blade – Joel Hinz Feb 4 '16 at 10:21
   
Thanks! I'll take a look – Tom P Feb 4 '16 at 12:40

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

Simply do @includeIf('some-view')

shareimprove this answer
 

来自 https://stackoverflow.com/questions/35198014/check-if-a-view-exists-and-do-an-include-in-laravel-blade


普通分类: