We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Runway

Runway Tip: Simple list/detail with dynamic title and 404 check

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:

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 subpage.php, 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.

subpage.php

<?php

// DETAIL
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
else {
  perch_layout('header');
  perch_collection('Collection',[
    'template'=>'post_in_list.html'
  ]);

}

perch_layout('footer');
Shane Lenzen

Shane Lenzen 18 points

  • 1 year ago

Great stuff, very useful. Thanks Shane.