28

I have layouts.app.blade.php where I have my <html> and <body> tags and also the <nav>.
In the <body> I yield content for every page, so they basically extend this app.blade.php.
All basic Laravel stuff so now I have this:

 <div class="navbar-header">
    <!-- Collapsed Hamburger -->
    <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#spark-navbar-collapse">
        <span class="sr-only">Toggle Navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>
    <!-- Branding Image -->
    <a class="navbar-brand" href="/">
        *Dynamic page title*
    </a>
</div>
// ...
@yield('content')

And I would like to use this <a class="navbar-brand"> to display my pagetitle. So this means it has to change for each template that is loaded (with @yield('content')) in this 'parent.blade.php'.

How would I do this using Laravel 5.2?

Many thanks

1
  • Augmenting the above solution on using the variable in your view, if you are extending a layout(you should), then you can render it like so if you have a title section: @section("title","$letter"). Thanks to the power of Blade. Hope this is useful. Jan 13, 2019 at 22:39

6 Answers 6

92

If this is your master page title below

<html>
<head>
    <title>App Name - @yield('title')</title>
</head>
<body>
    @section('sidebar')
        This is the master sidebar.
    @show

    <div class="container">
        @yield('content')
    </div>
</body>

then your page title can be changed in your blade page like below

@extends('layouts.master')

@section('title', 'Page Title')

@section('sidebar')
@parent

<p>This is appended to the master sidebar.</p>
@endsection

@section('content')
<p>This is my body content.</p>
@endsection

More information can be found here Laravel Docs

2
  • 2
    Oh well, that's pretty straight forward. Works like a charm.
    – nclsvh
    Dec 28, 2015 at 19:01
  • 1
    I was always trying to name the titles from controllers but now I see that yours is better. Thanks for the tip! Jul 8, 2019 at 10:37
7

You can pass it to a view for example

Controller

$title = 'Welcome';

return view('welcome', compact('title'));

View

isset($title) ? $title : 'title';

or php7

$title ?? 'title';

Null coalescing operator

5

Another alternative that has worked for me in Laravel 9 and PHP 8 is to use a prop in a master layout. In my project I have only one layout so it's simple:

<x-layout :title="' - Page Title'">
  <!-- content -->
</x-layout>

Then in the layout.blade.php file:

@props(['title'])
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Static Title{{$title ?? ""}}</title>
</head>
<body>
  <main>
    {{$slot}}
  </main>
</body>
</html>
1
3

Define this title in this format as given below in the main file like as (master.blade.php)

<title>Vendor | @yield('mytitle') </title>

on page here All Company is my new title for the blade file you want to dynamic title on this page

@section('mytitle', 'All Company')
1

in your controller declare a variable called title & compact it as below

    public function aboutUs()
    {
        //page title
        $title = 'Project | About Us';
        return view('project.about_us', compact('title'));
    }

in your view page call the variable as below

<title>{{ $title }}</title>

0

Your can try to put @stack('title') instead of the title tag in you header.blade.php file and then put this in your page.blade.php file:

@push('title')
    <title> Home Page</title>
@endpush
@section('main-container')
Your content
@endsection

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.