Page title by filtering via routes

Hi,

I am using filtering via routes ( https://getkirby.com/docs/cookbook/content/filter-via-route ). So I have the following url structure:

/projects (all items)
/projects/interior (interior items)
/projects/design (design items)
etc.

In my head snippet I get the page title with the following code:

<title><?= $page->title() ?> - <?= $site->title() ?></title>

My problem is that every page has the same ‘projects’ page title. So if I’m on a specific category, this ‘projects’ title still comes back.

/projects -> pagetitle: Projects - Site title
/projects/interior -> pagetitle: Projects - Site title
/projects/design -> pagetitle: Projects - Site title
etc.

It should be:

/projects -> pagetitle: Projects - Site title
/projects/interior -> pagetitle: Interior - Site title
/projects/design -> pagetitle: Design - Site title
etc.

Does anybody know how I can solve this?

Thanks!

You could create a specific snippet for the projects-template where you check if you are in a category and echo the category name instead.

1 Like

Or if you would like to have 1 generic head snippet you could also check if you are on this template and to logic there. E.g.:

<?php if($page->intendedtemplate() === "projects"): ?>
        <title><?= $page->title() ?> <?php e($category != "" , ":".$category, "") ?> - <?= $site->title() ?></title>
<?php else: ?>
    <title><?= $page->title() ?> - <?= $site->title() ?></title>
<?php endif ?>

PS: I assume $category is set earlier (e.g. in a controller).

1 Like

Hi @bvdputte! Thanks for you help :pray:

Somehow I get an error:
Cannot access protected property

However if I try the following code it I get the name of the template.

<?php
echo $page->intendedTemplate();
?>

Oh my fault I think I got it:

<?php if($page->intendedTemplate() === "projects"): ?>
        <title><?= $page->title() ?> <?php e($category != "" , ":".$category, "") ?> - <?= $site->title() ?></title>
<?php else: ?>
    <title><?= $page->title() ?> - <?= $site->title() ?></title>
<?php endif ?>

Forgot the () after intendedTemplate

But I got the same title without category. My projects.php controler is:

<?php

return function ($page, $category) {

    $projects = $page->children()->listed();

    if ($category) {
        $projects = $projects->filterBy('category', $category, ',');
    }

    return [
        'projects' => $projects
    ];

};

and config route:

'routes' => [
    [
        'pattern' => 'projects/(:any)',
        'action' => function ($category) {
            if ($page = page('projects/' . $category)) {
                return $page;
            } else {
                return page('projects')->render([
                    'category' => $category
                ]);
            }

        }
    ],

Yes, you’re right, I forgot the parenthesis. :see_no_evil:

Now it should work, I think?

Hmm. I got the same ‘Project’ title without the category name.

Your controller does not pass the $category variable to the template…

How can I do this? Sorry I don’t quite get it :frowning: . I am just starting out with php.

You’ve been around for quite a while, so I don’t really believe this :wink:

Like you pass the projects in the return array:

 return [
        'projects' => $projects,
        'category' => $category,
    ];

That’s right. Unfortunately for me that doesn’t mean I understand everything. I wish it were like that.

It still feels like I don’t understand PHP very well. I know the basics, and I know how to hack around the basics of Kirby. But I don’t really understand what I am doing exactly yet.

Unfortunately I am unable to get it to work.

In head:

<title><?= isset( $category ) ? Str::ucfirst( $category ) : $page->title() ?> - <?= $site->title() ?></title>

In controller:

<?php

return function ($page, $category) {

    $projects = $page->children()->listed();

    if ($category) {
        $projects = $projects->filterBy('category', $category, ',');
    }

    return [
        'projects' => $projects,
        'category' => $category,
    ];

};
1 Like

Hi @texnixe,

Thanks for your help and explanation. :grinning:

Have a great weekend!