If Active tag, add class

It’s late here and my google-skills are lacking behind.

I have a fairly simple question in regards to a filter-by-tag menu.

I want the active tag to get a class of “active” when it’s selected.

<aside class="primary__nav">

<?php
// fetch all tags
$tags = $site->page('projecten')->children()->listed()->pluck('tags', ',', true);
?>

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

</aside>
<?php $activeTag = param('tag'); ?>

<aside class="primary__nav">

<?php
// fetch all tags
$tags = $site->page('projecten')->children()->listed()->pluck('tags', ',', true);
?>

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

</aside>

I think there is a typo in this line:

<li<?= $activeTag ?? ' class="active" : '' ?>>

But I can’t see where exactly.

Missing closing quote, sorry:

<li<?= $activeTag ?? ' class="active"' : '' ?>>

for some reason this does not seem to work. I get this ParseError:

syntax error, unexpected token ":", expecting "," or ";"

When I change ‘??’ to ‘?’ the error disappears, but this doesn’t add the class either.

That was really stupid of me :see_no_evil:


<li<?= $activeTag === $tag ? ' class="active"' : '' ?>>
1 Like