php Laravel - 检查@yield 是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20412738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Check if @yield empty or not
提问by user2840318
Is it possible to check into a blade view if @yield have content or not?
如果@yield 有内容,是否可以检查刀片视图?
I am trying to assign the page titles in the views:
我正在尝试在视图中分配页面标题:
@section("title", "hi world")
So I would like to check in the main layout view... something like:
所以我想检查主布局视图......类似于:
<title> Sitename.com {{ @yield('title') ? ' - '.@yield('title') : '' }} </title>
采纳答案by Collin James
There is probably a prettier way to do this. But this does the trick.
可能有一种更漂亮的方法来做到这一点。但这确实有效。
@if (trim($__env->yieldContent('title')))
<h1>@yield('title')</h1>
@endif
回答by cborgia
In Laravel 5 we now have a hasSectionmethod we can call on a Viewfacade.
在 Laravel 5 中,我们现在有一个hasSection可以在ViewFacade上调用的方法。
You can use View::hasSectionto check if @yeildis empty or not:
您可以使用View::hasSection检查是否@yeild为空:
<title>
@if(View::hasSection('title'))
@yield('title')
@else
Static Website Title Here
@endif
</title>
This conditional is checking if a section with the name of title was set in our view.
这个条件是检查是否在我们的视图中设置了名称为 title 的部分。
Tip: I see a lot of new artisans set up their title sections like this:
提示:我看到很多新工匠设置他们的标题部分是这样的:
@section('title')
Your Title Here
@stop
but you can simplify this by just passing in a default value as the second argument:
但是你可以通过传递一个默认值作为第二个参数来简化这个:
@section('title', 'Your Title Here')
The hasSectionmethod was added April 15, 2015.
该hasSection方法于2015 年 4 月 15 日添加。
回答by 2Fwebd
For those looking on it now (2018+), you can use :
对于那些现在(2018+)查看它的人,您可以使用:
@hasSection('name')
@yield('name')
@endif
回答by Pathros
Given from the docs:
从文档中给出:
@yield('section', 'Default Content');
Type in your main layout e.g. "app.blade.php", "main.blade.php", or "master.blade.php"
输入您的主要布局,例如“app.blade.php”、“main.blade.php”或“master.blade.php”
<title>{{ config('app.name') }} - @yield('title', 'Otherwise, DEFAULT here')</title>
And in the specific view page (blade file) type as follows:
并在具体查看页面(刀片文件)输入如下:
@section('title')
My custom title for a specific page
@endsection
回答by passioncoder
You can simply check if the section exists:
您可以简单地检查该部分是否存在:
if (isset($__env->getSections()['title'])) {
@yield('title');
}
And you can even go a step further and pack this little piece of code into a Blade extension: http://laravel.com/docs/templates#extending-blade
你甚至可以更进一步,将这一小段代码打包到 Blade 扩展中:http: //laravel.com/docs/templates#extending-blade
回答by Maxim Abdalov
@if (View::hasSection('my_section'))
<!--Do something-->
@endif
回答by iSoft
Complete simple answer
完成简单的回答
<title> Sitename.com @hasSection('title') - @yield('title') @endif </title>
回答by Nodtem66
I have a similar problem with the solution:
我的解决方案也有类似的问题:
@section('bar', '')
@hasSection('bar')
<div>@yield('bar')</div>
@endif
//Output
<div></div>
The result will be the empty <div></div>
结果将是空的 <div></div>
Now, my suggestion, to fix this, is
现在,我的建议是解决这个问题
@if (View::hasSection('bar') && !empty(View::yieldContent('bar')))
<div>@yield('bar')</div>
@endif
回答by Malta
Use View::hasSectionto check if a section is defined and View::getSectionto get the section contents without using the @yieldBlade directive.
使用View::hasSection来检查,如果部分被定义并View::getSection得到部分内容,而无需使用@yield刀片指令。
<title>{{ View::hasSection('title') ? View::getSection('title') . ' - App Name' : 'App Name' }}</title>
回答by Kjell
Sometimes you have an enclosing code, which you only want to have included in that section is not empty. For this problem I just found this solution:
有时您有一个封闭的代码,您只想包含在该部分中的代码不为空。对于这个问题,我刚刚找到了这个解决方案:
@if (filled(View::yieldContent('sub-title')))
<h2>@yield('sub-title')</h2>
@endif
The title H2 gets only displayed it the section really contains any value. Otherwise it won't be printed...
标题 H2 仅显示该部分确实包含任何值。否则打印不出来...

