eriktobben's avatar

Using Blade "echo" to display a page title

Hi!

I am passing a variable named $customer to a view. I can use Blade to display the data using {{ $customer->name }} inside the current view, that works great.

But I have a section that connects to the master template file, using it to set a H3 header and also the pages <title> in the master file.


In my show.blade.php:
@section('title', '{{ $customer->name }}')
@section('title_header', '{{ $customer->name }}')

In master.blade.pho:
<title><title>@yield('title') </title></title>
<h3 class="title">@yield('title_header')</h3> 

The output in browser is just:
name); ?>

Is there another way of doing this?

0 likes
8 replies
zanematthew's avatar

Hi,

@eriktobben yes there is another way of doing this, at least this is how I circumvented your (and mine) dilemma.

<!-- layouts/master.blade.php -->

<title>My Website | {{ $page_title or 'Welcome' }}</title>

In your controller set it as you wish. $page_title => $customer->name

Leveraging the or syntax in Laravels blade means you do not need to handle this via a @section() call.

willvincent's avatar

I do it like this:

@if (!empty($meta['title']))
   <title>{{ $meta['title'] }} - mysite.com</title>
@else
   <title>Some Default Title</title>
@endif

This way from any controller method I can pass an array meta with whatever meta tags I want to populate into the header of the page.. so like meta description, meta keywords, canonical link, etc.. the page specific view just ignores that, generally.. but the variable then gets passed up and made available to the layout template.

You could also then do something like this in your layout's header:

@if (isset($meta))
  @foreach ($meta as $type => $content)
  <meta {{ $type }}="{{ $content }}" />
  @endforeach
@endif
2 likes
Salomoni's avatar

Pretty old post but.. I think it's view responsibility decide what's shown in the title. So I like the way @eriktobben handled this.

You can also add callback string if the section is not provided in sub-view.

<!-- layouts/master.blade.php -->

<title>My Website | @yield('title', 'Welcome')</title>
<!-- about.blade.php -->

@section('title', 'About us')
2 likes
zanematthew's avatar

@Salomoni his approach wasn't working, since you cannot pass variables into yields, or sections. Hence the alternatives posted above.

Salomoni's avatar

@zanematthew Yes you can. But I still recommend to let views themselves decide what is shown in the title than controllers.

<!-- routes.php -->

Route::get('/about', function () {
    $title = 'About us';
    
    return view('about', compact('title'));
});
<!-- layouts/master.blade.php -->

<title>My Website | @yield('title', 'Welcome')</title>
<!-- about.blade.php -->

@extends('layouts.master')

@section('title', $title)

or echo $title as callback if not override in sub-view.

<!-- layouts/master.blade.php -->

<title>My Website | @yield('title', $title)</title>
2 likes
COTIGA's avatar

Hi, Like PHP concatenation

@section('title', 'A '.$salon->ville.', salon d\'art contemporain art3f du '.$salon->datebeb.' au '.$salon->datefin)
1 like
prappo_p's avatar

Use just like PHP

@section('title', $customer->name )

3 likes

Please or to participate in this conversation.