(Sorry)
I had time to try this out only today.
I am trying to sluggify
a foreach
loop outputting tags.
I tried to simplify and adjust the code from this post to my needs, but it’s not working.
In blog.php
's controller I have:
return function($site, $pages, $page) {
// get all articles
$articles = $page->children()->visible()->flip();
// fetch all tags
$topics = $articles->pluck('topics', ',', false);
//add tag filter (`topic`)
if(!empty(param('topics'))) {
$topics = str::slug(param('topics'));
$articles = $articles->filter(function($article) use($topics) {
// split tags and convert to lowercase
$topics = array_map('str::slug', $article->topics()->split(','));
});
}
return compact('articles', 'topics', 'topic');
};
and in the blog.php
template:
<?php foreach($topics as $topic): ?>
<div class="c-tag-collection__tag-name filter" data-filter=".<?php echo html($topic) ?>">
<?php echo html($topic) ?>
</div>
<?php endforeach ?>
Is it better to add slug::
in the controller or directly in the template?
What am I doing wrong?