Separate tags by comma

I use this code to show tags:

if($tag = param('tag')) {
    $tag = urldecode(param('tag'));
    $articles = $articles->filterBy('tags', $tag, ',');
}
$tags = $articles->pluck('tags', ',', false);

<?php foreach($tags as $tag): ?>
    <a href="<?= url( url::paramsToString(['tag' => $tag])) ?>" rel="category tag"><?= $tag ?></a>
<?php endforeach ?>

And I’m trying to separate tags by comma using implode.

Any idea of how can I implement it?

Hm, I don’t quite see where imploding tags would fit in the above context?

If you have an array of tags, you can of course implode it:

$tags = $articles->pluck('tags', ',', false);
$list = implode(',', $tags);
echo $list;

Well, the idea is to show tags separate by comma.

I see that using <?= implode(', ', $tags); ?> I get the list of tags separate by comma, but I don’t know how to include it in the foreach loop.

Or maybe there is a simpler way to do it.

That’s what I meant, it does not make sense to include it in the foreach loop, otherwise you get a comma separated list of tags for each loop, but no links. But since you want a link around each tag, a simple comma separated list of tags does not make sense? In the example above, I would include the comma via CSS.

a[rel~="category"]:not(:last-child):after {
  content: ', ';
}

Yes, I was looking for a php method to do it but your CSS solution is simple and practical.

Thanks

For completeness sake, here is a PHP version:

<?php
$tags = page('projects')->children()->pluck('tags', ',', false);

function wrapMe($tag)
{
  return '<a href="' . url( url::paramsToString(['tag' => $tag])) . '" rel="category tag">' . $tag . '</a>';
}

$tags = array_map("wrapMe", $tags);

echo implode(',', $tags);
?>

I’d strongly recommend using the CSS solution, though.

Yes, the CSS solution is better.

And it makes sense, after all it’s a matter of presentation.

Well, maybe, but the question is if the drop in performance is really noticeable, compared to all the other stuff.

The point here was to use a selector that already existed in the DOM instead of introducing another selector, i.e. a new class.