Mitch
Mitch

Reputation: 1394

Laravel 4 how to apply title and meta information to each page with blade master page

Trying to apply an individual title and meta description to my websites pages, but I'm not sure if the way I'm attempting is very clean.

master.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>{{ $title }}</title>
    <meta name="description" content="{{ $description }}">
</head>

individual page

@extends('layouts.master')
<?php $title = "This is an individual page title"; ?>
<?php $description = "This is a description"; ?>

@section('content')

I feel like this is a quick and dirty way to get the job done, is there a cleaner way?

Upvotes: 30

Views: 27520

Answers (5)

Jquestions
Jquestions

Reputation: 1730

If you want to use a variable in your title so it's dynamically generated from your DB, I do it like this:

master.blade.php

<title>@yield('title')</title>

article.blade.php

@section( 'title', '' . e($article->title) )

It uses https://laravel.com/docs/5.7/helpers#method-e

Upvotes: 0

Paolo Resteghini
Paolo Resteghini

Reputation: 432

Really recommend this:

https://github.com/artesaos/seotools

You pass the information to the the view require the content

SEOTools::setTitle($page->seotitle);
SEOTools::setDescription($page->seodescription);

Upvotes: 2

zeckdude
zeckdude

Reputation: 16163

This works as well:

master.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
    <title>@yield('title')</title>
    <meta name="description" content="@yield('description')">
</head>

individual page

@extends('layouts.master')

@section('title')
    This is an individual page title
@stop

@section('description')
    This is a description
@stop

@section('content')

or if you want to shorten that some more, alternately do this:

individual page

@extends('layouts.master')

@section('title', 'This is an individual page title')
@section('description', 'This is a description')

@section('content')

Upvotes: 88

user2020432
user2020432

Reputation: 39

no one think that the best way is to create your own class with facade (Site::title(), Site::description etc) and mutators (via Str::macro) that automatically check if title, description etc is in right format (max length, adding categories, defaults, separators, etc) and clone data to others fields (title => og:title, description => og:description) if necessary?

Upvotes: 1

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

This should work:

@extends('layouts.master')
<?php View::share('title', 'title'); ?>

...

You can also do this:

@extends('views.coming-soon.layout', ['title' => 'This is an individual page title'])

Upvotes: 9

Related Questions