i am batman
i am batman

Reputation: 679

Laravel yield doesn't show the contents

Yield functions doesn't load particular sections in other files.

Route

Route::get('/', function () {
    return view('add-listing');
});

add-listing.blade.php

@extends('layout')

@section('body')
  This is add-listing page
@stop

header.blade.php

@extends('layout')

@section('header')
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <title>Find Do Responsive Directory Template</title>
    <link rel="stylesheet" type="text/css" href="css/master.css">
    <link rel="stylesheet" type="text/css" href="css/color-green.css">
    <link rel="stylesheet" type="text/css" href="css/app.css">
    <link rel="shortcut icon" href="images/short_icon.png">
@stop

layout.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    @yield('header')
</head>
<body>

    @yield('body')

</body>

When I run this only the @section('body') content is loading. It doesn't load the @section('header'). Why is that?

Upvotes: 0

Views: 1692

Answers (2)

ramin ashrafimanesh
ramin ashrafimanesh

Reputation: 1062

In this example you call add-listing.blade.php and in this file just you said call layout.blade so your blades don't call header.blade just prepare a section for get header contents and you don't call header.blade in your flow.

Yield is such as a variable that can be set by section:

@yield('header') ~ echo $header

@section('header')

header value is here

@endsection

So $header = header value is here.

Upvotes: 0

tyro
tyro

Reputation: 1428

You may use @include('header') in layout :

<!DOCTYPE html>
<html lang="en">
<head>
    @include('header')
</head>
<body>

    @yield('body')

</body>

header.blade.php :

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <title>Find Do Responsive Directory Template</title>
    <link rel="stylesheet" type="text/css" href="css/master.css">
    <link rel="stylesheet" type="text/css" href="css/color-green.css">
    <link rel="stylesheet" type="text/css" href="css/app.css">
    <link rel="shortcut icon" href="images/short_icon.png">

Upvotes: 1

Related Questions