Using `page` helper within custom route

Hey! :wave:

This is my first time using routes to generate complete pages within an application, rather than just serving JSON or chunks of HTML.

I was hoping to include some logic within my global header snippet to add classes to the body element based on “real page” templates/slugs/URIs (i.e. pages that represent content in the content folder).

However, when calling page() in my helper function, it defaults to the homepage, which I would consider misleading—if there isn’t actually a page in the content folder, it seems like this should return false, instead. It’s not likely that this behavior can be incorporated into core, since some projects may actually depend on the default return value…

This manifests in something like this, for an “app” page…

<body class="kiosk home default">

…instead of…

<body class="kiosk">

…for which kiosk is suppled when including the header snippet.

So, the problem arises when trying to determine whether the value returned from page() is a result of it falling back to home, or if it’s a genuine value. My current assumption is that hard-coding a comparison between the current request URL and that of the homepage is the only way to handle this. Ideas?

The code that generates the above looks like this:

<? class Help {
  public static function body_classes ($classes = []) {
    if ($page = page()) {
      array_push($classes, $page->uid());
      array_push($classes, $page->template());

    return implode(' ', array_unique($classes));

…with the header snippet including…

<? /* Picks up any classes provided when including the header snippet */ ?>
<body class="<?= Help::body_classes(isset($bodyClasses) ? $bodyClasses : []) ?>">

…and it’s include on a normal template looking like…

<? /* Uses default UID and template classes */ ?>
<? snippet('header') ?>

…and on an app page…

<? /* Supply custom classes */ ?>
<? snippet('header', ['bodyClasses' => ['kiosk']) ?>

Because these classes are used for JS initialization and to apply CSS, it’s important that the classes represent only what should actually be there…