Laravel - Check if @yield empty or not

后端 未结 17 1912
醉梦人生
醉梦人生 2021-01-31 02:08

Is it possible to check into a blade view if @yield have content or not?

I am trying to assign the page titles in the views:

@section(\"title\", \"hi wor         


        
相关标签:
17条回答
  • 2021-01-31 02:18

    Use View::hasSection to check if a section is defined and View::getSection to get the section contents without using the @yield Blade directive.

    <title>{{ View::hasSection('title') ? View::getSection('title') . ' - App Name' : 'App Name' }}</title>
    
    0 讨论(0)
  • 2021-01-31 02:20

    Complete simple answer

    <title> Sitename.com @hasSection('title') - @yield('title') @endif </title>
    
    0 讨论(0)
  • 2021-01-31 02:20

    why not pass the title as a variable View::make('home')->with('title', 'Your Title') this will make your title available in $title

    0 讨论(0)
  • 2021-01-31 02:23
    @hasSection('content')
      @yield('content')
    @else
      \\Something else
    @endif
    

    see "Section Directives" in If Statements - Laravel docs

    0 讨论(0)
  • 2021-01-31 02:26

    The way to check is to not use the shortcut '@' but to use the long form: Section.

    <?php
      $title = Section::yield('title');
      if(empty($title))
      {
        $title = 'EMPTY';
      }
    
      echo '<h1>' . $title . '</h1>';
    ?>
    
    0 讨论(0)
  • 2021-01-31 02:28

    Building on Collin Jame's answer, if it is not obvious, I would recommend something like this:

    <title>
      {{ Config::get('site.title') }} 
      @if (trim($__env->yieldContent('title')))
        - @yield('title')
      @endif
    </title>
    
    0 讨论(0)
提交回复
热议问题