Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel dynamic page title in navbar-brand

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

like image 867
nclsvh Avatar asked Dec 28 '15 16:12

nclsvh


People also ask

What is yield in laravel?

In Laravel, @yield is principally used to define a section in a layout and is constantly used to get content from a child page unto a master page.


2 Answers

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

like image 166
PhillipMwaniki Avatar answered Sep 27 '22 20:09

PhillipMwaniki


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

like image 25
Erik Avatar answered Sep 27 '22 20:09

Erik