【问题标题】:Laravel - Check if @yield empty or notLaravel - 检查@yield 是否为空
【发布时间】:2013-12-23 03:29:32
【问题描述】:

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

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

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

所以我想检查主布局视图...类似于:

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

【问题讨论】:

  • 为什么不将标题作为变量传递

标签: php laravel laravel-4


【解决方案1】:

对于那些现在(2018 年以上)看它的人,你可以使用:

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

见:https://laravel.com/docs/5.6/blade#control-structures

【讨论】:

  • 请注意,文档在 5.2 和 5.4 之间的某个时间点被删除,但在 5.5 中又被添加回来。
【解决方案2】:

在 Laravel 5 中,我们现在有一个 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')

 

添加了hasSection方法April 15, 2015

【讨论】:

  • 这绝对是 Laravel 5+ 的做法——超级简单。
【解决方案3】:

可能有一种更漂亮的方法可以做到这一点。但这可以解决问题。

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

【讨论】:

  • 为此创建一个新的刀片指令会更漂亮,而不是使用代码中的隐藏变量。欲了解更多信息:laravel.com/docs/5.3/blade#extending-blade
  • 现在hasSection 这要好得多,您可以考虑编辑此答案。 (见:stackoverflow.com/a/36671780/2666094
  • 如果我必须申请 OR 怎么办?像 hasSection('section1' 或 'section2')?有什么事吗?
  • 堆栈/推送元素是否有类似的技术?
【解决方案4】:

根据文档:

@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

【讨论】:

  • 这是最佳实践,解决了问题的根本问题,更具可读性和(部分)效率更高。
  • 没有。 OP 想要检查参数,以便他们可以在模板中添加“-”字符。这个语法和答案没有涵盖它。
【解决方案5】:
@hasSection('content')
  @yield('content')
@else
  \\Something else
@endif

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

【讨论】:

    【解决方案6】:

    您可以简单地检查该部分是否存在:

    if (isset($__env->getSections()['title'])) {
    
        @yield('title');
    }
    

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

    【讨论】:

    【解决方案7】:

    完整的简单答案

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

    【讨论】:

    • 是的,只是想提供一个完整的工作答案,如果有要显示的标题,则只显示“-”。认为如果他们可以剪切和粘贴可能会节省其他人的时间
    • 感谢您回答问题仔细阅读问题。具体来说,这个问题要问的是什么?确保您的答案提供了这一点
    【解决方案8】:
    @if (View::hasSection('my_section'))
        <!--Do something-->
    @endif
    

    【讨论】:

      【解决方案9】:

      使用View::hasSection 检查是否定义了一个部分,使用View::getSection 获取部分内容,而不使用@yield Blade 指令。

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

      【讨论】:

        【解决方案10】:

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

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

        检查是否缺少部分:

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

        【讨论】:

          【解决方案11】:

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

          @section('bar', '')
          @hasSection('bar')
          <div>@yield('bar')</div>
          @endif
          //Output
          <div></div>
          

          结果将是空的&lt;div&gt;&lt;/div&gt;

          现在,我的建议是解决这个问题

          @if (View::hasSection('bar') && !empty(View::yieldContent('bar')))
          <div>@yield('bar')</div>
          @endif
          

          【讨论】:

            【解决方案12】:

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

            【讨论】:

              【解决方案13】:

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

              View::composer('*', function($view)
              {
                  $title = Config::get('app.title');
              
                  $view->with('title', $title ? " - $title" : '');
              });
              

              【讨论】:

                【解决方案14】:

                你可以不做吗:

                layout.blade.php

                &lt;title&gt; Sitename.com @section("title") Default @show &lt;/title&gt;

                在 subtemplate.blade.php 中:

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

                【讨论】:

                  【解决方案15】:

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

                  <?php
                    $title = Section::yield('title');
                    if(empty($title))
                    {
                      $title = 'EMPTY';
                    }
                  
                    echo '<h1>' . $title . '</h1>';
                  ?>
                  

                  【讨论】:

                  • 如果没有解释为什么这被否决,它对任何人都没有帮助。
                  • 可能不是刀片式的。
                  • 我不同意;特别是在 Laravel 3 'blade.php' 中指出:'Blade @yield 语句是 Section::yield 方法的快捷方式。为自己的目的提取功能并没有错。
                  • 测试这似乎对我不起作用。我会选择$__env-&gt;getSections()['title']
                  • Laravel 从那时起就不断发展,如果有更好的方法,我不会感到惊讶。这篇文章现在已经快两年了,落后了两个版本。
                  【解决方案16】:

                  基于 Collin Jame 的回答,如果不是很明显,我会推荐如下内容:

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

                  【讨论】:

                    【解决方案17】:

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

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

                    标题 H2 仅在该部分确实包含任何值时才显示。否则不会打印出来...

                    【讨论】:

                      猜你喜欢
                      • 2020-02-04
                      • 2016-06-20
                      • 2018-12-25
                      • 1970-01-01
                      • 2015-02-13
                      • 2017-05-07
                      • 2021-05-30
                      • 2019-06-09
                      • 1970-01-01
                      相关资源
                      最近更新 更多