Active link on filters

Hi,
I try to get an active link on my filters but whatever I do I never see the active class in the <a>

my controller

<?php
return function($site, $pages, $page) {
  $projects = page('projects')->children()->visible();
  $tags = $projects->pluck('artiste', ',', true);
    $collator = new Collator('fr_CA');
    $collator->sort($tags);
	if($tag = param('index')) {
	  	$projects = $projects->filterBy('artiste', $tag, ',');
	}
  	return compact('projects', 'tags');
};
?>

And my menu links

 <ul>
     <?php $pageTags = $page->tags()->split(); ?>
        <?php $p = kirby()->request()->params()->tag(); ?>
        <?php foreach($tags as $tag): ?>
        <li>
            <a <?= in_array($tag, $pageTags)? 'class="active"':'' ?> href="<?php echo url('index/' . url::paramsToString(['index' => $tag])) ?>">
                <?php echo html($tag) ?>
            </a>
        </li>
        <?php endforeach ?>
</ul>

Where is that menu snippet?

If the menu links are on the same page as the controller, $pageTags will likely be empty, because the tags are on the children, not on the parent?

The menu is in a “index-panel.php” snippet wich is included in a template called “index.php”. But if I move the snippet into the template like you supposed to do, it doesn’t change anything.

Maybe you can post your page structure, where are you using which template/controller etc. otherwise it is difficult to help. I don’t know which page your index.php relates to.

I see what you mean but my goal is to collect $tag from an other page called projects as you could see in my controller
Do you mean that ? because doesn’t work too

<?= in_array($p, $tag)? 'class="active"':'' ?>

As I assumed, your page does collect some subpages and you want to use filters.

But what you are doing is to check if a particular tag from the current page is in the $tags array. This will never result in an active link.

You need to check if $p (the currently requested tag) is the same as $tag.

Yes, I try to collect $tag from projects children pages like you could see in controller.
But <?= in_array($p, $tag)? 'class="active"':'' ?> doesn’t work too

$tag is not an array, so you can’t use in_array(), you need to check if they are equal (if($p == $tag))

Sorry I’m such a noob that I don’t even know what is an array. Could you be more specfic ?
I tried <?php if($p == $tag, 'class="active"') ?>

Using the e() helper:

<?php e($p == $tag, 'class ="active"') ?>
$array = array('apple', 'orange', 'pear');
$string = 'apple';
<?php  $tags = $projects->pluck('artiste', ',', true); ?>

would give you an array of values.

But when you loop through the array of tags, a single $tag is just a string.

If you want, you can test this by using the gettype() method:

echo gettype($tag);
  1. When I include echo gettype($tag); anywhere, it doesn’t return anything
  2. <?php e($p == $tag, 'class ="active"') ?> outside my link return class=“active”
  3. <?php e($p == $tag, 'class ="active"') ?> inside my a doesn’t return anything

:confounded:

Well, it was just meant as a help for future debugging; of course you have to put that after $tag is defined, i.e. within the foreach loop, not just anywhere.

Yes I did

<?php foreach($tags as $tag): ?>
        <?php gettype($tag); ?> // 1st try : nothing
        <li>
            <a href="<?php echo url('index/' . url::paramsToString(['index' => $tag])) ?>">
                <?php echo html($tag) ?> <?php gettype($tag); ?> // 2nd try : nothing
            </a>
        </li>
        <?php endforeach ?>

I really considering that I’m not good enough at coding and maybe ask someone in the “JOBS questions” for my future project instead of broken your time with my questions

Well, there is an echo missing…

Also, in your definition of $p you are trying to get the tag parameter, whereas you are using index, so it should be:

<?php $p = kirby()->request()->params()->index(); ?>

Yes, I get “STRING” with <?php $p = kirby()->request()->params()->index(); ?> and <?php echo gettype($tag); ?>

Your code should now look like this:

<ul>
  <?php foreach($tags as $tag): ?>
    <?php $p = kirby()->request()->params()->index();?>
    <?php foreach($tags as $tag): ?>
      <li>
         <a <?php e($tag == $p, 'class="active"') ?> href="<?php echo url('index/' . url::paramsToString(['index' => $tag])) ?>">
            <?php echo html($tag) ?>
         </a>
      </li>
  <?php endforeach ?>
</ul>

Exactly, and I get String if I include <?php echo gettype($tag); ?>
The active class doesn’t appear

What do you get when you echo $p?

<?php $p = kirby()->request()->params()->index();
echo $p;
?>

I get my list of every ‘artiste’ from <?php $tags = $projects->pluck('artiste', ',', true); ?>

That’s not what I mean. When you click on a link to filter the projects, and only then, you get an index parameter in the URL. When you have an index parameter in the URL, $p should have the value of that “tag”. Only then can a tag be active.