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

Mitch picture Mitch · Feb 16, 2014 · Viewed 24.9k times · Source

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?

Answer

zeckdude picture zeckdude · Sep 18, 2014

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')