@yield

Renders a named section from a layout, with an optional fallback when nothing filled it.

Signature
@yield('name', 'Default content')

Parameters

ParameterTypeDescription
$namestringName of the section to render at this point in the layout.
$defaultoptionalstring= ''Rendered when the section being yielded is undefined.

Return value

stringThe content of the named section, or the default value when no section with that name was defined.

Errors

Never throws — an undefined section renders the default value, or nothing when no default was given.

@yield marks the point in a layout where a child view's @section of the same name is rendered. It is the receiving half of Blade's template inheritance: @section captures, @yield emits.

Defaults

The second argument is rendered when no section with that name exists — @yield('title', 'Dashboard'). Use it for anything a page may reasonably omit, so a new view does not have to define five sections just to render.

Yield versus section

A layout can also declare content with @section ... @show, which defines a default and renders it in one place, letting a child override it or extend it with @parent. Use @yield when the layout has nothing to contribute and the child owns the block entirely; use @section ... @show when there is a sensible default with markup.

What trips people up

Section content is not escaped at yield time — @yield emits what the section produced, and escaping happened (or did not) inside the section itself. A default passed as the second argument is likewise printed as given, so never build one from request input.

A misspelled name fails silently: the section is captured, nothing yields it, and the page renders without it.

Example

Example · php
<?php

// resources/views/layouts/app.blade.php
$layout = <<<'BLADE'
<html>
    <head>
        <title>App Name - @yield('title', 'Dashboard')</title>
    </head>
    <body>
        <div class="container">
            @yield('content')
        </div>
    </body>
</html>
BLADE;

// resources/views/reports.blade.php — defines content but no title
$child = <<<'BLADE'
@extends('layouts.app')

@section('content')
    <h1>Monthly report</h1>
@endsection
BLADE;

echo view('reports')->render();
// <html><head><title>App Name - Dashboard</title></head>
// <body><div class="container"><h1>Monthly report</h1></div></body></html>

When to use it

  • Drop the page body into the container div of a shared layout
  • Give the <title> tag a per-page value with a sensible site-wide default
  • Let individual pages inject extra stylesheets into the layout's head

Frequently asked questions

How do I set a default value for a Blade @yield?

Pass it as the second argument: @yield('content', 'Default content'). It renders whenever no section with that name has been defined.

What is the difference between @yield and @section ... @show?

@yield only renders whatever a child section supplied, while @section ... @show defines default content in the layout and renders it on the spot. Use @show when the layout has real default markup a child may override or extend with @parent.

Why is my Blade section not showing up in the layout?

Almost always a name mismatch between @section and @yield — an undefined section renders the default, or nothing, without an error. Check the spelling, and confirm the child actually extends the layout doing the yielding.

More examples

Yield versus @section ... @show

@yield('content') renders nothing when no child fills it. @section ... @show renders the master sidebar by default and still allows a child to replace or extend it, which is why layouts use @show for blocks that have real default markup and @yield for empty holes.

Example · php
<?php

// Layout: the child owns this block entirely.
$yielded = <<<'BLADE'
<div class="container">
    @yield('content')
</div>
BLADE;

// Layout: default content lives here and renders immediately.
$shown = <<<'BLADE'
@section('sidebar')
    This is the master sidebar.
@show
BLADE;

// A child can override the sidebar, or extend it with @parent.
$child = <<<'BLADE'
@extends('layouts.app')

@section('sidebar')
    @parent
    <p>Plus one page-specific link.</p>
@endsection
BLADE;

echo view('child')->render();

Yielding a section that was never defined

The stylesheet never appears: the child defined 'style' and the layout yields 'styles'. Nothing errors, because a missing section is a legitimate state that falls back to the default. When a block silently fails to render, comparing the two spellings is the first check.

Example · php
<?php

// resources/views/layouts/app.blade.php
$layout = <<<'BLADE'
<head>
    @yield('styles')
    <title>@yield('title', 'App Name')</title>
</head>
BLADE;

// Child with a typo in the section name.
$child = <<<'BLADE'
@extends('layouts.app')

@section('style')
    <link rel="stylesheet" href="/css/report.css">
@endsection
BLADE;

echo view('child')->render();
// <head>
//     <title>App Name</title>
// </head>

Escaping is the section's job

The inline second argument of @section is emitted verbatim by @yield, so a page title containing markup is injected into the layout as HTML. The block form is safe because {{ }} escapes on the way in. This is the usual route by which a user-editable title becomes an XSS hole inside a <title> tag.

Example · php
<?php

// Safe: the section escapes the value it captures.
$safe = <<<'BLADE'
@section('title', e($page->title))
BLADE;

// Also safe: the block form uses {{ }}.
$alsoSafe = <<<'BLADE'
@section('title')
    {{ $page->title }}
@endsection
BLADE;

// Unsafe: raw user input reaches @yield untouched.
$unsafe = <<<'BLADE'
@section('title', $page->title)
BLADE;

echo view('pages.show', ['page' => $page])->render();

Discussion

  • Be the first to comment on this lesson.