A layout is a view file, which is extended by other views which inject blocks of code into their parent. For example:
parent.blade.php:
<html>
<head>
<style type='text/css'>
@yield('styling')
</style>
</head>
<body>
<div class='main'>
@yield('main-content')
</div>
</body>
</html>
child.blade.php:
@extends('parent')
@section('styling')
.main {
color: red;
}
@stop
@section('main-content')
This is child page!
@stop
otherpage.blade.php:
@extends('parent')
@section('styling')
.main {
color: blue;
}
@stop
@section('main-content')
This is another page!
@stop
Here you see two example child pages, which each extend the parent. The child pages define a @section
, which is inserted in the parent at the appropriate @yield
statement.
So the view rendered by View::make('child')
will say "This is child page!" in red, while View::make('otherpage')
will produce the same html, except with the text "This is another page!" in blue instead.
It is common to separate the view files, e.g. having a layouts folder specifically for the layout files, and a separate folder for the various specific individual views.
The layouts are intended to apply code that should appear on every page, e.g. adding a sidebar or header, without having to write out all the html boilerplate in every individual view.
Views can be extended repeatedly - i.e. page3 can @extend('page2')
, and page2 can @extend('page1')
.
The extend command uses the same syntax as used for View::make
and @include
, so the file layouts/main/page.blade.php
is accessed as layouts.main.page
.