Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lestah's avatar
Level 11

@yield is not working

on my main.blade.php

@yield('header')

on my home.blade.php


@section('header')

<header class="masthead" style="background-image: url('template/img/home-bg.jpg')">
    <div class="overlay"></div>
    <div class="container">
      <div class="row">
        <div class="col-lg-8 col-md-10 mx-auto">
          <div class="site-heading">
            <h1>Clean Blog</h1>
            <span class="subheading">A Blog Theme by Start Bootstrap</span>
          </div>
        </div>
      </div>
    </div>
  </header> 

@endsection

what could be wrong?

0 likes
49 replies
Lestah's avatar
Level 11

@MUNAZZIL - oh yeah that was just a typo i got no ('header') on my end section when i check the sourcepage @yield is not working

Lestah's avatar
Level 11

@MUNAZZIL - I've tried this

<div>
    @yield('header')
</div>

still not displaying the header on my main.blade.php

munazzil's avatar

Please change a name and check becuse header is common name. example head1

Lestah's avatar
Level 11

its on my views folder as well as my main.blade.php

RonB1985's avatar

What you have should work. main.blade.php and home.blade.php are in the same folder? In /views/? No subfolder?

Lestah's avatar
Level 11

yes my main.blade.php and my home.blade.php is both on my views folder no subfolder

RonB1985's avatar

So you have something like this in your home.blade.php:

@extends('main')

@section('header')

Bla bla

@endsection

And your route is returning the home view?

Lestah's avatar
Level 11

@RONB1985 - yes i did that on my home.blade.php but when i run checked the sour file its blank

RonB1985's avatar

Try removing the <header class= and so on, and just put some text in between:

@section('header')

This is my header

@endsection

See if that works at all. I suspect it's got something to do with that header html code

Lestah's avatar
Level 11

on my


@section('header')

    <p>this is my header</p>

    @endsection 

The this is my header paragraph is not showing as well

RonB1985's avatar

Hmm strange.. Perhaps your views are cached or something? Just to try..

php artisan cache:clear
php artisan view:clear
Lestah's avatar
Level 11

@RONB1985 - yeah i agree i dont know what cud be wrong when i use @yield.. but when i undo the @yield it displays the page with no errors or anything. i did clear as well cache and view still not working

Snapey's avatar

You are checking the page source, not visually what it looks like?

so, just to check where we are;

main.blade.php contains layout and a @yield('section_name'). Also main.blade.php does not have @extends

Next, home.blade.php contains @extends('main') and @section('section_name') followed by @endsection

and both files are in the root of your resources\views folder?

and all filenames and references are in lowercase?

jlrdw's avatar

Section, yield, etc are convenience methods added, you could just skip and get straight to the point on what you need to include as example:

<?= View::make('partials/dpag')->render(); ?>
I can pop such as this in a template or view, doesn't matter.

No messing with section  end section, yield,  etc.

Just use your blade if so desired. Dig into the API to find out more.

MwSpaceLLC's avatar

I do not know why it does not work for you. but in my projects for the pieces of code I use a simple include

because for the components you do not simply use in home.blade.php:

@include('header')

and in the header.blade.php file, put:

<header class = "masthead" style = "background-image: url ('template / img / home-bg.jpg')">
     <div class = "overlay"> </ div>
     <div class = "container">
       <div class = "row">
         <div class = "col-lg-8 col-md-10 mx-auto">
           <div class = "site-heading">
             <h1> Clean Blog </ h1>
             <span class = "subheading"> A Blog Theme by Start Bootstrap </ span>
           </ Div>
         </ Div>
       </ Div>
     </ Div>
   </ Header>
Snapey's avatar

@jlrdw why woukd you mess about with that bx when yield and section work perfectly?

jlrdw's avatar

Because

<?= View::make('partials/dpag')->render(); ?>

Is same thing but so much easier.

Also:

        $view = 'dog/index';
        $layout = ViewLayout::getLayout('dog/indextp');  //custom code
        $content = View::make($view)
                ->with('dogs', $dogs);
        return view($layout)->with('content', $content)->with('title', $title);

So return layout, not view and in layout:

<?php echo $content; ?>

content is the view. Make first in controller.

That is so much easier than yield, section, or whatever.

Besides if section, yield, etc was so easy, why so many questions on it here.

And with all due respect:

that bx

came from Taylor's API documentation.

I never checked prior, but from vendor folder:

$echo = "<?php echo $__env->make({$expression}, \Illuminate\Support\Arr::except(get_defined_vars(), array('__data', '__path')))->render(); ?>";

You are just rendering whatever.

Cronix's avatar

@jlrdw Ok, but OP was asking specifically about blade templates, so it would be best to actually help with that, if you're going to help, rather than give an answer the OP wasn't asking for. Most people here want to use laravel, and learn laravel. It doesn't really help much to answer a question that wasn't asked.

It's cool if you don't really know how to use blade and avoid it. No sweat, but most others don't want to do that. You really should actually learn it someday. You don't know what you're missing.

1 like
Cronix's avatar

@lestah Please show your complete main.blade.php and home.blade.php files. Not some of the content, all of it.

jlrdw's avatar

But it's php. See example from vendor folder. I just do that direct. It is the exact same thing, you are rendering something at a certain place in the template.

It would be easier not to have yield, section, and just render what's needed where needed.

It does not hurt for folks to learn some things from the API, that's actually easier.

In fact unless you have an entire HTML page for the view, the flow should be:

  • return layout
  • in layout render whatever and the main view, via
<?= $content; ?>

Or the blade equal I guess:

{{ $content }}  is that correct.

I am not trying to talk mean here, but some of the blade files asked here on forum look like old basic spaghetti code, not clean at all. Whereas just rendering what's needed makes for cleaner code.

I'm not saying don't use blade, I'm saying there is no need for section, and yield.

Cronix's avatar

Still, that's not what the OP asked regardless if you think it's better or not.

jlrdw's avatar

@cronix that's true, I agree. But if OP ask for a piece of fish, may as well teach how to fish at same time.

Cronix's avatar

I don't think that analogy is applicable here. Should we expect a php dissertation whenever someone asks specifically about laravel stuff because it's all php behind the scenes? I assume they know php (how to fish) if they are using laravel. Now they want to use a net and be more efficient, so it's a step backwards and wastes time to teach them how to fish (again) when they are wanting (and asking for) the next step.

jlrdw's avatar

@cronix I am not trying to argue here but what you said is 180 degrees off. Look at what happens in the vendor folder, that is truly the net.

Not those shortcuts. It's the shortcuts many times that fumble up newcomers to the framework.

Instead of a direct approach to what's really happening in the background.

Furthermore as I said I am not saying don't use blade I'm just saying you can render direct, it's less code and a heck of a lot easier.

Next

Please or to participate in this conversation.