Ad

Change Page Title Inside Another Include - Laravel

- 1 answer

I'm just starting to learn Laravel and want to know how to go about doing the below. I'll give the code then explain.

I have a file includes/head.blade.php. this file contains things you find inside the <head>. So it contains <title>@yield('title')</title> If I now include this file in a page let say pages/about.blade.php like this @include('includes.head'), How then can I modify the <title> nested inside the include using this line @section('title', ' bout Us')

Ad

Answer

If you include the blade file like @include('includes.head') then you cannot do <title>@yield('title')</title> in head.blade.php . Correct way to do this is passing the value while including the file like :

@include('includes.head',['title'=>'About Us'])

and in head.blade.php you must do like:

<title>
   @if(isset($title))
       {{ $title }}
   @endif 
</title>

But if you extends the heade.blade.php then you can do like this :

head.blade.php

<title>@yield('title')</title>

about.blade.php

@extends('includes.head')
@section('title')
    {{ "About Us" }}
@endsection

For more info Check this

Ad
source: stackoverflow.com
Ad