Highlighting an active tag

My blogpage includes a tagcloud, using code from the Kirby cookbook:

<ul class="tags">
   <?php foreach($tags as $tag): ?>
    <li>
      <a href="<?= url($page->url(), ['params' => ['tag' => $tag]]) ?>">
        <?= html($tag) ?>
      </a>
    </li>
   <?php endforeach ?>
 </ul>

I would like to highlight the tag that is currently active. But when I add

<?php e($item->isOpen(), 'aria-current="page" ') ?>

to style it using a[aria-current] like it is used in the Starter Kit code for the header-nav, the page goes offline “due to an unexpected error”.

How can I create a list of tags where the selected tag can be highlighted with css?

Two issues:

  1. $item is nowhere defined, you cannot just use a random variable anywhere without defining it first (in the header-nav, we are looping through a collection of pages and $item is defined as the value in that loop, in that case, a page object)
  2. isOpen() is a page method, in the nav snippet from where you got that code, $item is a page object, but in the current case, we are just dealing with an array of tags, where the single tag ($tag) is just a string.

So to determine if a tag is active, we have to compare the $tag in the loop with the current tag parameter in the URL:

<a href="<?= url($page->url(), ['params' => ['tag' => $tag]]) ?>" <?= $tag === param('tag') ? 'class="active"' : '' ?>>

Thanks a lot!

Unfortunately, the suggested line also causes the page to go offline. However, I found a former topic that helped solving the issue (sorry for missing that before).

The code that actually worked goes like this:

<a href="<?= url($page->url(), ['params' => ['tag' => $tag]]) ?>" <?php e($tag === param('tag'), 'class="active"', '') ?>><?= html($tag) ?></a>

What I did not communicate clearly enough though was that I want the selected tag to be styled differently. To achieve this, I replaced class="active" by class="current" and added css accordingly. Now everything works as I want it to:

<ul class="tags">
		<?php foreach($tags as $tag): ?>
			<li>
	      <a href="<?= url($page->url(), ['params' => ['tag' => $tag]]) ?>" <?php e($tag === param('tag'), 'class="current"', '')  ?>><?= html($tag) ?></a>
	    </li>
    <?php endforeach ?>
</ul>