Umair Mehmood
Umair Mehmood

Reputation: 562

How can I add dynamic page title in Laravel

I want to add a dynamic page title like I have a post list and when I click on a single post details page open, in that case, I want that dynamic title to be displayed in head title file.

@extends('layouts.app')
@section('title', 'This is {{$post->title}} Post Page')
@section('contents')
<h3>{{$post->title}}</h3>
<p>{{$post->body}}</p>
@endsection

You can see in this section. @section('title', 'This is {{$post->title}} Post Page') {{$post->title}} but its not working.. its showing something like this.

This is title); ?> Post Page

And on app.blade.php I have something like this.

<title>Laravel Practice - @yield('title')</title>

Upvotes: 3

Views: 6298

Answers (2)

ceejayoz
ceejayoz

Reputation: 180004

Blade's variable syntax isn't valid in a @section call, so you'll want to use plain old PHP:

@section('title', 'This is ' . $post->title . ' Post Page')

If $post->title is user input, you're going to want to escape things to be safe from XSS vulnerabilities:

@section('title', 'This is ' . e($post->title) . ' Post Page')

Upvotes: 10

MirzaS
MirzaS

Reputation: 512

You can't leave {{$post->title}} inside '', just put it out and connect with '.' like this :

@section('title', 'This is' . {{$post->title}} . 'Post Page')

Upvotes: -1

Related Questions