Overview
Most commonly this section/yield approach is used to create a global area in your layout that can be changed by your templates. This eliminates the need for any brittle and messy logic.
Cheatsheet:
-
No thank you:
{{ if template == "news" }} hardcode something {{ /if }}
-
Yes please:
{{ section:something }}
+{{ yield:something }}
Example
In the example below, everything within the section:sidebar
tag will not be rendered in the template, but rather in the layout.
// The Template <h1>{{ title }}</h1>{{ content }} {{ section:sidebar }} <h2>About the Author</h2> <div> {{ author:name }} </div> {{ author:bio }}{{ /section:sidebar }}
// The Layout<html> <head> <title>{{ title }} | {{ site_name }}</title> </head> <body> <article> {{ template_content }} </article> <aside> {{ yield:sidebar }} </aside> </body></html>
// The Template@extends('layout') <h1>{{ $title }}</h1>{!! $content !!} @section('sidebar') <h2>About the Author</h2> <div> {{ $author['name'] }} </div> {{ $author['bio'] }}@endsection
// The Layout<html> <head> <title>{{ $title }} | {{ $site_name }}</title> </head> <body> <article> {!! $template_content !!} </article> <aside> @yield('sidebar') </aside> </body></html>
Related Reading
If you haven't read up on templates and layouts, you should. It's relevant.