How to highlight an active filter?

Hi there,
I’ve followed your amazing youtube course building a portfolio website, and I would like to add a CSS class to an active filter. I’m an absolute beginner, so I don’t really understand how to do that.

In the youtube comments, you said to ‘in the foreach loop for the filter buttons you can compare $filter with $filterBy and then add a css class or an aria-current attribute if they are the same’

but I can’t seem to make it work. I guess it should go somewhere here, but I’m not sure.

  <nav class="filter">
    <a href="<?= $page->url() ?>">All</a>
  <?php foreach ($filters as $filter): ?>
    <a href="<?= $page->url() ?>?filter=<?= $filter ?>"><?= $filter ?></a>
  <?php endforeach ?>
  </nav>

Thank you so much!

Hey, welcome to our forum. Assuming you are using this code: kirbycasts/projects.php at main · getkirby/kirbycasts · GitHub

<nav class="filter">
      <a href="<?= $page->url() ?>">All</a>
      <?php foreach ($filters as $filter): ?>
      <a href="<?= $page->url() ?>?filter=<?= $filter ?>" class="<?= $filterBy === $filter ? 'active' : '' ?>"><?= $filter ?></a>
      <?php endforeach ?>
    </nav>

Great, thank you! I would like to highlight the “All” Link if no Filter is active. Is there a possibility to check if no filter is selected to add the “active” class to this Link?
Thank you!