如何在仅存在刀片模板的情况下 @包含它?我可以这样做:
@if (File::exists('great/path/to/blade/template.blade.php'))
@include('path.to.blade.template')
@endif
Run Code Online (Sandbox Code Playgroud)
但那真的不优雅高效.
我可以在没有if语句的情况下包含它,如果文件不在这里,则捕获并隐藏错误,但如果不是野蛮的话,这有点脏.
什么会是伟大的是这样的:
@includeifexists('path.to.blade.template')
Run Code Online (Sandbox Code Playgroud)
(伪代码,但此刀片命令不存在)
yan*_*gqi 21
您可以View::exists()用来检查视图是否存在.
@if(View::exists('path.to.view'))
@include('path.to.view')
@endif
Run Code Online (Sandbox Code Playgroud)
或者您可以扩展刀片并添加新指令
Blade::directive('includeIfExists', function($view) {
});
Run Code Online (Sandbox Code Playgroud)
查看官方文档:http://laravel.com/docs/5.1/blade#extending-blade