美文网首页
Laravel 5 判断 @yield('xxx'

Laravel 5 判断 @yield('xxx'

作者: BiaoWong | 来源:发表于2018-12-11 14:38 被阅读34次

代码:

方法1:

// 判断 @section('xxx') 是否已定义
@hasSection('xxx')
    @yield('xxx')
@else
    # 其他
@endif

说明:没什么可说的。

方法2:

// 判断 @yield('xxx') 是否存在
// 其中“xxx”是@section('xxx', '值')定义的key
@if (trim($__env->yieldContent('xxx')))
    # 存在
@else
    # 不存在
@endif

使用场景:

自定义模板 title, keywords 和 description 时。

例如:

模板头文件

@if (trim($__env->yieldContent('title')))
    <title>@yield('title')_其他内容</title>
@else
    <title>其他内容</title>
@endif
<meta name="keywords" content="@yield('keywords')">
<meta name="description"ccontent="@yield('description')">

访问页模板

@section('title', '访问页面的标题')
@section('keywords', '访问页面的关键字')
@section('description', '访问页面的描述')

说明:
模板头文件的 @yield('title') 会获取到访问页模板 @section('title', '访问页的标题') 定义的 title 对应的值,也就是 访问页的标题

总结

两种方法都能解决同样的问题,具体使用哪种还得看个人爱好啦😁。

相关文章

网友评论

      本文标题:Laravel 5 判断 @yield('xxx'

      本文链接:https://www.haomeiwen.com/subject/kloshqtx.html