Laravel - 检查@yield是否为空

时间:2013-12-05 23:11:08

标签: php laravel laravel-4

如果@yield有内容,是否可以检查刀片视图?

我正在尝试在视图中分配页面标题:

@section("title", "hi world")

所以我想检查一下主要的布局视图......如:

<title> Sitename.com {{ @yield('title') ? ' - '.@yield('title') : '' }} </title>

17 个答案:

答案 0 :(得分:56)

在Laravel 5中,我们现在可以在 <a href="http://twitter.com/home/?status=Don\'t talk about yourself\; it will be done when you leave. -Wilson Mizner" class="btn btn-default btn-primary twitter">Tweet</a> 外观上调用hasSection方法。

您可以使用View检查View::hasSection是否为空:

@yeild

此条件是检查我们的视图中是否设置了标题名称的部分。

提示:我看到很多新工匠设置了这样的标题部分:

<title>
    @if(View::hasSection('title'))
        @yield('title')
    @else
        Static Website Title Here
    @endif
</title>

但你可以通过传入一个默认值作为第二个参数来简化这个:

@section('title')
Your Title Here
@stop

添加了@section('title', 'Your Title Here') 方法April 15, 2015

答案 1 :(得分:43)

对于现在正在寻找的人(2018+),你可以使用:

if var sentBeacons = metrics["sentBeacons"] as? UInt {
    sentBeacons += 1
}

请参阅:https://laravel.com/docs/5.6/blade#control-structures

答案 2 :(得分:38)

这可能是一种更漂亮的方法。但这就是诀窍。

@if (trim($__env->yieldContent('title')))
    <h1>@yield('title')</h1>
@endif

答案 3 :(得分:15)

来自文档:

@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

答案 4 :(得分:6)

您只需检查该部分是否存在:

if (isset($__env->getSections()['title'])) {

    @yield('title');
}

您甚至可以更进一步将这一小段代码打包到Blade扩展中:http://laravel.com/docs/templates#extending-blade

答案 5 :(得分:1)

activeStatus: Joi.any().valid(1, 2, 3, [1, 2, 3])
// Method no longer accepts array arguments: valid'

请参见If Statements - Laravel docs中的“部分指令”

答案 6 :(得分:1)

Laravel 7.x的新功能-sectionMissing()

@hasSection('name')
   @yield('name')
@else
   @yield('alternative')
@endif

检查是否缺少该部分:

@sectionMissing('name')
   @yield('alternative')
@endif

答案 7 :(得分:1)

在不使用View::hasSection Blade指令的情况下,使用View::getSection检查是否定义了节,并使用@yield获取节的内容。

<title>{{ View::hasSection('title') ? View::getSection('title') . ' - App Name' : 'App Name' }}</title>

答案 8 :(得分:1)

@if (View::hasSection('my_section'))
    <!--Do something-->
@endif

答案 9 :(得分:0)

以Collin Jame的答案为基础,如果不明显,我会推荐这样的话:

<title>
  {{ Config::get('site.title') }} 
  @if (trim($__env->yieldContent('title')))
    - @yield('title')
  @endif
</title>

答案 10 :(得分:0)

检查的方法是不使用快捷键'@',而是使用长格式:Section。

<?php
  $title = Section::yield('title');
  if(empty($title))
  {
    $title = 'EMPTY';
  }

  echo '<h1>' . $title . '</h1>';
?>

答案 11 :(得分:0)

有时候您有一个封闭的代码,您只想包含在该部分中就不为空。对于这个问题,我刚刚找到了以下解决方案:

@if (filled(View::yieldContent('sub-title')))
    <h2>@yield('sub-title')</h2>
@endif

标题H2仅显示在该部分确实包含任何值的情况下。否则它将无法打印...

答案 12 :(得分:0)

完整的答案

<title> Sitename.com @hasSection('title') - @yield('title') @endif </title>

答案 13 :(得分:0)

我的解决方案也有类似的问题:

@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

答案 14 :(得分:0)

你不能这样做:

layout.blade.php

<title> Sitename.com @section("title") Default @show </title>

在subtemplate.blade.php中:

@extends("layout")

@section("title") My new title @stop

答案 15 :(得分:0)

我认为你不能,但你有选择,比如使用视图编辑器总是为你的观点提供一个$ title:

View::composer('*', function($view)
{
    $title = Config::get('app.title');

    $view->with('title', $title ? " - $title" : '');
});

答案 16 :(得分:0)

为什么不将标题作为变量View::make('home')->with('title', 'Your Title')传递,这将使您的标题在$title

中可用
相关问题