如何仅在刀片模板存在时才扩展刀片模板?
问题描述:
基本上我只想在刀片模板存在时扩展刀片模板,如果不是@扩展不同的模板。关于使用@if @endif块有几个堆栈溢出答案,但只有在@include文件而不是@extending时才有效。理想情况是这样的,但它不起作用:如何仅在刀片模板存在时才扩展刀片模板?
@if(some_condition == true)
@extends('one')
@else
@extends('two')
@endif
如果唯一的方法是使用Blade指令,可否请您提供一个示例?谢谢!
答
试着做这样的:
@extends($somecondition == true ? 'one' : 'two')
+0
工程就像一个魅力 - 谢谢! –
答
@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)
更多信息
答
您可以使用View::exists
@if (View::exists('one'))
@extends('one')
@else
@extends('two')
@endif
或
file_exists()
并获取路径使用resource_path()
此,
@if (file_exists(resource_path('views/one.blade.php')))
@extends('one')
@else
@extends('two')
@endif
你可以尝试这个,在我的情况下工作..看到Laravel文档 - https://laravel.com/docs/5.5/views
你可以检查文件是否存在使用PHP。 'file_exists($ filename)'。我不知道是否存在一个Laravel方法来检查这个 –
我自己做了类似的事情,但不是使用@extends,而是必须使用@include('page')',然后在它保存@extends的页面文件中。 – Option