Check if @yield('something') is set

tbergeron posted 4 years ago


I'm trying to use @yield for my page titles, that way I could simply @section A Cool Title @stop within my templates to add a title to my page.

I tried with Environment::yieldContent('something') ( ) but I guess it's not how it's supposed to work hehe

Basically what I'd like to do is:

<title>Website Name @if(@yield('something')) : @endif @yield('something')</title>

This doesn't work since @ inside @ opens 2 php tags and removing the @ from yield simply executes a non existing function.

Any ideas how I could do this?


tobysommer replied 4 years ago Solution


You can pass a second parameter to the yield function. From the docs (

Sometimes, such as when you are not sure if a section has been defined, you may wish to pass a default value to the @yield directive. You may pass the default value as the second argument:

@yield('section', 'Default Content');

So you can do something like

<title>My Awesome Site - @yield('title', 'since 2008')</title>

So whenever there is a title section within your view, its content will be populated. Otherwise the default will be used (which also can be an empty string of course)

nztim replied 4 years ago

It might not be precisely what you want, but using the aforementioned second parameter you could do this:

<title>Website Name : @yield('page_title', 'Tagline goes here')</title>

EDIT: After I posted I saw your revision tobysommer

tbergeron replied 4 years ago

Both sounds like great suggestions but they aren't direct solution to my question.

I don't want to put a default value in case there's none, I need to know how to check if it returns something.

Still, I wasn't aware of that second parameter for @yield() so thanks a lot, I'm sure this will me anyways :)

wppd replied 4 years ago

I have encountered this similar problem before.

I end up with View composer

	class MetaComposer{

		public function compose($view)
			if ( ! empty($view['title']) )
			    $view['title'] .= ' | '.$site_title;
			    $view['title'] = $site_title;
			return $view;
jos- replied 4 years ago

You can use this in your view:

@if (array_key_exists('mySection', View::getSections()))
jwpage replied 4 years ago

I came up with a slightly icky solution to this:

@if ($__env->yieldContent('title')
    | My Site
   Welcome to my wonderful website!
Torann replied 3 years ago

I'm using this helper function in my Laravel 5 apps:

 * Checks whether a section has been captured yet.
 * @param  string  $section
 * @return bool
function content_for($section)
    return array_key_exists($section, app('view')->getSections());
jeremykenedy replied 3 years ago
<title>@if (trim($__env->yieldContent('template_title')))@yield('template_title') | @endif {{ Lang::get('') }}</title>


<title>@if (trim($__env->yieldContent('template_title')))@yield('template_title') | @endif Global Website Title</title>
phuongngoc replied 2 years ago

Too much work, simply use:

@if (View::hasSection('something'))
do something
do nothing

P/S: Please have a look into "vendor/laravel/framework/src/Illuminate/View/Factory.php"

adriaanzon replied 2 years ago

It's even simpler now. Use the @hasSection directive:

        @yield('title') - App Name
        App Name
Arcrammer replied 2 years ago

Wow. Thank you, adriaanzon! Is there any way to use @hasSection in a ternary?

adampatterson replied 1 year ago

I have been doing this for about 2 years now and this is my implementation.

<title>@yield('body.title', 'default title') | Always here</title>

Then in my templates used

@section('body.title', 'Contact Us')

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us