Collections
Extending Parent Templates
To display each of your collection items on their own page, you need to specify a parent template. You can do this in the extends
key of the YAML front matter, or with the @extends
directive in a Blade file:
my-first-post.md
1---2extends: _layouts.post3title: My First Blog Post4author: Keith Damiani5date: 2017-03-236section: content7---8 9This post is *profoundly* interesting.
my-second-post.blade.php
1---2title: My Second Blog Post3author: Keith Damiani4date: 2017-03-255section: content6---7@extends ('_layouts.post')8 9This is {{ $page->author }}'s second <strong>amazing</strong> post.
Collection items with no parent template
However, parent templates are optional for collection items. In some cases—such as for a collection of staff bios that appear on an "About Us" page—you may not need to display each of your collection items on their own pages. To do this, simply omit the extends
key from the YAML front matter, or the @extends
directive from a Blade file.
Collection items with multiple parent templates
Collection items can also extend multiple parent templates, by specifying the templates as an array in the extends
key in the YAML front matter. This will generate a separate URL for each template—allowing, for example, a collection item to have both /web/item
and /api/item
endpoints, or /summary
and /detail
views.
_people/abraham-lincoln.md
1--- 2name: Abraham Lincoln 3role: President 4number: 16 5extends: 6 web: _layouts.person 7 api: _layouts.api.person 8section: content 9---10...
_layouts.person.blade.php
1@extends('_layouts.master') 2 3@section('body') 4 <header> 5 <h1>{{ $page->name }}</h1> 6 <h2>{{ $page->role }}</h2> 7 </header> 8 9 @yield('content')10@endsection
_layouts.api.person.blade.js
1{!! $page->api() !!}
If using multiple parent templates, you can specify separate paths in config.php
for each resulting page:
config.php
1<?php 2 3use Illuminate\Support\Str; 4 5return [ 6 'collections' => [ 7 'people' => [ 8 'path' => [ 9 'web' => 'people/{number}/{filename}',10 'api' => 'people/api/{number}/{filename}',11 ],12 'api' => function ($page) {13 return [14 'slug' => Str::slug($page->getFilename()),15 'name' => $page->name,16 'number' => $page->number,17 'content' => $page->getContent(),18 ];19 },20 ],21 ],22];