Filtering pages via param() — i.e. tag:keyword in URL …

Dear Kirby community,

again, I’m a little stuck for quite some while now. I’d like to display some links to pages, filtered via param(), for example /tag:keyword.

Here’s the links to the docs: Filtering with tags and filterBy()

When visiting example.com/semesters, I get a link list of all semesters from a field in content/1_semesters/semesters.txt. That is the second foreach loop in the code below and it’s working.

The listed links — when clicked — append a tag to the URL, e.g. example.com/semesters/semester:2018-ws … where 2018-ws is the keyword I’d like to filter by. Appending the tag and keyword works, but no content is shown. I assume there’s an issue in the if or the first foreach loop.

My folder structure is the following:

content/1_semesters/semesters.txt

content/2_students/first-student-name/student.txt

content/2_students/first-student-name/first-project/project.txt
content/2_students/first-student-name/second-project/project.txt

content/2_students/second-student-name/student.txt

content/2_students/second-student-name/first-project/project.txt
content/2_students/second-student-name/second-project/project.txt

[…]

My semesters.php file:

<?php if($filter = param('semester')): ?>
<?php foreach ($site->index()->filterBy('template', 'project')->filterBy('semester', $filter) as $project): ?>
      <a class="project block" href="<?= $project->url() ?>">
        <h3><?= $project->project() ?></h3>
        <p><?= $project->text()->kirbytext() ?></p>
      </a>
<?php endforeach ?>
<?php else: ?>
<?php foreach ($page->semesters()->toStructure()->sortBy('id', 'desc') as $semester): ?>
      <a class="semester block" href="<?= $page->url() . "/semester:" . $semester->id() ?>">
        <h3><?= $semester->topic() ?></h3>
        <span>&mdash;&thinsp;<?= $semester->semester() ?></span>
      </a>
<?php endforeach ?>
<?php endif ?>

Thank you very much in advance for your advice and help.

You check if a parameter is set, but you don’t apply the $filter variable anywhere?

Whoops, removed it for debugging.

The filter is the first foreach loop, second line of the last code block above.

<?php foreach ($site->index()->filterBy('template', 'project')->filterBy('semester', $filter) as $project): ?>

project.txt looks like:

Project: Project Title

----

Semester: 2018-ws

I also tried, instead of $site->index()->filterBy('template', 'project')->…, the following method. Same results, so probably that’s not the problem.

<?php foreach ($site->find('project')->filterBy('semester', $filter) as $project): ?>

Hmm … seems I cannot filterBy('template', 'project') … but I don’t understand, why. Each “project” in content/2_students/student-name/some-project-title/ has a project.txt inside.

What works: filterBy('template', 'student')->children()

Any idea why that’s the case?

do you have a site/blueprints/pages/project.yml and site/templates/project.php?

1 Like

Yes, both files exist. (Though I’m not even sure if they HAVE to, if the .txt file is named project.txt?)

PS: Mhm, maybe templates/project.php didn’t yet exist yesterday … so it seems you are right — the template files called like the text files HAVE to exist.