Reputation: 2164
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')
Upvotes: 2
Views: 1434
Reputation: 21901
I think you can use the @include
like this, check this DOC.
@include('includes.head', ['title' => 'About Us'])
and the title
should be print as,
<title>{{ $title }}</title>
FOR the best practice
Check the laravel blade templating
feature,
you can define a master layout
, extending that layout you can create new views. As like in the this DOC.
master.blade.php
<html>
<head>
<title>@yield('title')</title>
</head>
<body>
@section('sidebar')
This is the master sidebar.
@show
<div class="container">
@yield('content')
</div>
</body>
</html>
about.blade.php
@extends('master')
@section('title', 'About Us') // this will replace the **title section** in master.blade
//OR
//@section('title')
// About Us
//@endsection
@section('sidebar')
<p>This is appended to the master sidebar.</p>
@endsection
@section('content')
<p>This is my body content.</p>
@endsection
Upvotes: 0
Reputation: 2284
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
Upvotes: 1