Laravel - Check if @yield empty or not
PhpLaravelLaravel 4Php Problem Overview
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 world")
So I would like to check in the main layout view... something like:
<title> Sitename.com {{ @yield('title') ? ' - '.@yield('title') : '' }} </title>
Php Solutions
Solution 1 - Php
For those looking on it now (2018+), you can use :
@hasSection('name')
@yield('name')
@endif
Solution 2 - Php
In Laravel 5 we now have a hasSection
method we can call on a View
facade.
You can use View::hasSection
to check if @yeild
is empty or not:
<title>
@if(View::hasSection('title'))
@yield('title')
@else
Static Website Title Here
@endif
</title>
This conditional is checking if a section with the name of title was set in our view.
Tip: I see a lot of new artisans set up their title sections like this:
@section('title')
Your Title Here
@stop
but you can simplify this by just passing in a default value as the second argument:
@section('title', 'Your Title Here')
The hasSection
method was added April 15, 2015.
Solution 3 - Php
There is probably a prettier way to do this. But this does the trick.
@if (trim($__env->yieldContent('title')))
<h1>@yield('title')</h1>
@endif
Solution 4 - Php
Given from the docs:
@yield('section', 'Default Content');
Type in your main layout e.g. "app.blade.php", "main.blade.php", or "master.blade.php"
<title>{{ config('app.name') }} - @yield('title', 'Otherwise, DEFAULT here')</title>
And in the specific view page (blade file) type as follows:
@section('title')
My custom title for a specific page
@endsection
Solution 5 - Php
@hasSection('content')
@yield('content')
@else
\\Something else
@endif
see "Section Directives" in If Statements - Laravel docs
Solution 6 - Php
You can simply check if the section exists:
if (isset($__env->getSections()['title'])) {
@yield('title');
}
And you can even go a step further and pack this little piece of code into a Blade extension: http://laravel.com/docs/templates#extending-blade
Solution 7 - Php
Complete simple answer
<title> Sitename.com @hasSection('title') - @yield('title') @endif </title>
Solution 8 - Php
@if (View::hasSection('my_section'))
<!--Do something-->
@endif
Solution 9 - Php
I have a similar problem with the solution:
@section('bar', '')
@hasSection('bar')
<div>@yield('bar')</div>
@endif
//Output
<div></div>
The result will be the empty <div></div>
Now, my suggestion, to fix this, is
@if (View::hasSection('bar') && !empty(View::yieldContent('bar')))
<div>@yield('bar')</div>
@endif
Solution 10 - Php
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>
Solution 11 - Php
New in Laravel 7.x -- sectionMissing()
:
@hasSection('name')
@yield('name')
@else
@yield('alternative')
@endif
Check if section is missing:
@sectionMissing('name')
@yield('alternative')
@endif
Solution 12 - Php
I don't think you can, but you have options, like using a view composer to always provide a $title to your views:
View::composer('*', function($view)
{
$title = Config::get('app.title');
$view->with('title', $title ? " - $title" : '');
});
Solution 13 - Php
why not pass the title as a variable View::make('home')->with('title', 'Your Title')
this will make your title available in $title
Solution 14 - Php
Can you not do:
layout.blade.php
<title> Sitename.com @section("title") Default @show </title>
And in subtemplate.blade.php:
@extends("layout")
@section("title") My new title @stop
Solution 15 - Php
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>';
?>
Solution 16 - Php
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>
Solution 17 - Php
Sometimes you have an enclosing code, which you only want to have included in that section is not empty. For this problem I just found this solution:
@if (filled(View::yieldContent('sub-title')))
<h2>@yield('sub-title')</h2>
@endif
The title H2 gets only displayed it the section really contains any value. Otherwise it won't be printed...