[英]Laravel - Check if @yield empty or not
如果@yield 有内容,是否可以检查刀片视图?
我正在尝试在视图中分配页面标题:
@section("title", "hi world")
所以我想检查主布局视图......类似于:
<title> Sitename.com {{ @yield('title') ? ' - '.@yield('title') : '' }} </title>
对于那些现在(2018+)查看它的人,您可以使用:
@hasSection('name')
@yield('name')
@endif
请参阅: https : //laravel.com/docs/5.6/blade#control-structures
在 Laravel 5 中,我们现在有一个可以在View
外观上调用的hasSection
方法。
您可以使用View::hasSection
来检查@yeild
是否为空:
<title>
@if(View::hasSection('title'))
@yield('title')
@else
Static Website Title Here
@endif
</title>
这个条件是检查是否在我们的视图中设置了名称为 title 的部分。
提示:我看到很多新工匠设置他们的标题部分是这样的:
@section('title')
Your Title Here
@stop
但是你可以通过传递一个默认值作为第二个参数来简化这个:
@section('title', 'Your Title Here')
hasSection
方法于2015 年 4 月 15 日添加。
可能有一种更漂亮的方法来做到这一点。 但这确实有效。
@if (trim($__env->yieldContent('title')))
<h1>@yield('title')</h1>
@endif
从文档中给出:
@yield('section', 'Default Content');
输入您的主要布局,例如“app.blade.php”、“main.blade.php”或“master.blade.php”
<title>{{ config('app.name') }} - @yield('title', 'Otherwise, DEFAULT here')</title>
并在具体查看页面(刀片文件)输入如下:
@section('title')
My custom title for a specific page
@endsection
您可以简单地检查该部分是否存在:
if (isset($__env->getSections()['title'])) {
@yield('title');
}
你甚至可以更进一步,将这一小段代码打包到 Blade 扩展中: http : //laravel.com/docs/templates#extending-blade
@hasSection('content')
@yield('content')
@else
\\Something else
@endif
请参阅If Statements - Laravel docs 中的“Section Directives”
完成简单的回答
<title> Sitename.com @hasSection('title') - @yield('title') @endif </title>
@if (View::hasSection('my_section'))
<!--Do something-->
@endif
使用View::hasSection
来检查是否定义了一个部分,使用View::getSection
来获取部分内容,而不使用@yield
Blade 指令。
<title>{{ View::hasSection('title') ? View::getSection('title') . ' - App Name' : 'App Name' }}</title>
Laravel 7.x 中的新功能—— sectionMissing()
:
@hasSection('name')
@yield('name')
@else
@yield('alternative')
@endif
检查部分是否丢失:
@sectionMissing('name')
@yield('alternative')
@endif
为什么不将标题作为变量传递View::make('home')->with('title', 'Your Title')
这将使您的标题在$title
可用
我不认为你可以,但你有选择,比如使用视图作曲家总是为你的视图提供 $title :
View::composer('*', function($view)
{
$title = Config::get('app.title');
$view->with('title', $title ? " - $title" : '');
});
你不能这样做:
layout.blade.php
<title> Sitename.com @section("title") Default @show </title>
在 subtemplate.blade.php 中:
@extends("layout")
@section("title") My new title @stop
检查的方法是不使用快捷方式“@”,而是使用长格式:Section。
<?php
$title = Section::yield('title');
if(empty($title))
{
$title = 'EMPTY';
}
echo '<h1>' . $title . '</h1>';
?>
基于 Collin Jame 的回答,如果不明显,我会推荐这样的:
<title>
{{ Config::get('site.title') }}
@if (trim($__env->yieldContent('title')))
- @yield('title')
@endif
</title>
有时您有一个封闭的代码,您只想包含在该部分中的代码不为空。 对于这个问题,我刚刚找到了这个解决方案:
@if (filled(View::yieldContent('sub-title')))
<h2>@yield('sub-title')</h2>
@endif
标题 H2 仅显示该部分确实包含任何值。 否则打印不出来...
我的解决方案也有类似的问题:
@section('bar', '')
@hasSection('bar')
<div>@yield('bar')</div>
@endif
//Output
<div></div>
结果将是空的<div></div>
现在,我的建议是解决这个问题
@if (View::hasSection('bar') && !empty(View::yieldContent('bar')))
<div>@yield('bar')</div>
@endif
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.