I do this for a lot of my sites, so I thought I'd post it here for reference. This is used for a URL structure where /subpage is the list URL and /subpage/name-of-item is the detail URL.

First, create your page in Runway and add a route like subpage/[slug:s]

I use a layouts header so I can set the <title> if necessary, like this:

Layout: header.php

<?php

if (perch_layout_has('title')) {
  $this_title = perch_layout_var('title', true);
} else {
  $this_title = perch_pages_title(true);
}
echo '<title>'.$this_title.' | My Cool Website</title>';

In the master page, for a detail page I check if the slug exists and 404 if it doesn't. If it does, I set the title and output the item. If this is a list page, I output the header without a set title, then output the collection list.

Master page: subpage.php

<?php

// DETAIL PAGE
if (perch_get('s')) {

  $result = perch_collection('Collection',[
    'filter'=>'slug',
    'match'=>'eq',
    'value'=>perch_get('s'),
    'skip-template'=>true,
    'return-html'=>true
  ],true);

  // CHECK 404
  if (empty(array_filter($result))) {
    PerchSystem::use_error_page(404);
    exit;
  }

  // POST EXISTS, OUTPUT PAGE
  else {
    perch_layout('header',[
      'title'=>$result[0]['title']
    ]);
    echo $result['html'];
  }
}

// LIST PAGE
else {

  perch_layout('header');
  perch_collection('Collection',[
    'template'=>'post_in_list.html'
  ]);

}

perch_layout('footer');
Categories: