如何仅在 blade 模板存在时扩展它?

How to EXTEND a blade template only if it exists?

基本上我想@extend一个blade模板只有当它存在时,如果不存在则@extend一个不同的模板。有一些关于使用@if @endif 块的堆栈溢出答案,但只有当你是@including 文件而不是@extending 时才有效。理想情况下是这样的,但它不起作用:

@if(some_condition == true)
    @extends('one')
@else
    @extends('two')
@endif

如果唯一的方法是使用 Blade 指令,您能举个例子吗?谢谢!

尝试这样做:

@extends( $somecondition == true ? 'one' : 'two')
@if( file_exists('path to file one'))
    @extends('one')
@else
    @extends('two')
@endif

你可以使用view:exists

@if(View::exists('path.to.view.one'))
    @extends('one')
@else
    @extends('two')
@endif

您可以使用条件定义要加载的视图名称,然后简单地扩展它,例如:

@php

 $view = '';

 if (some_condition == true) {
     $view = 'one';
 } else {
      $view = 'two';
 }

@endphp

...

@extends($view)

更多信息

https://laravel.com/docs/5.5/blade#php

您可以使用View::exists

@if (View::exists('one'))
    @extends('one')
@else
    @extends('two')
@endif

file_exists() 并获取路径使用 resource_path() this,

@if (file_exists(resource_path('views/one.blade.php')))
    @extends('one')
@else
    @extends('two')
@endif

你可以试试这个,在我的例子中,它是有效的。请参阅 Laravel - https://laravel.com/docs/5.5/views

中的文档