How to link tags?

Hi!

I have a portfolio page where every project has one or more tags.
The tags from each project should be shown both in an overview (foreach project: img, title, tags) and in the project page itself - plus i want to list all tags inside the footer which will be seen on every page.

      <a href="<?php echo url('projekte/tag:' . urlencode($tag)) ?>"><?php echo $tag ?>
  </a>

This is the link I put both in the template projects (the overview) and in the template project (the project page). The docs say that I need a controller but as far as I understand controller are template related… My tags should be clickable from everywhere and then be shown as a overview-like page, which has a title saying “tagname” and then showing all projects connected to this tag I initially clicked on.

so far… I hope you understand what I want to achieve and maybe someone can help me!
thanks!

Your links have to reference the page where you want to show your results, presumably the projects overview page, where you then either show all projects or projects filtered by tag.

You can do the filtering in a template, but it would be better if you use a controller, in your case that would be the projects.php controller for the projects.php template. As regards the filtering itself, there is a cookbook recipe to the rescue.

Edit: it does not matter where you want to show the tags as long as the links you generate are correct.

Hey thanks for your quick reply!

I got one follow-up php question then: is it possible to combine foreach and if statements?
Currently my projects/overview code snippet looks like this:

  <?php
$tags = $project->tags()->split();
  ?>

  <?php foreach(page('projekte')->children()->visible()->sortBy('year', 'desc') as $project): ?>




<div class="grid-item">
  <a href="<?php echo $project->url() ?>">
<?php if($image = $project->images()->sortBy('orientation', 'asc')->first()): ?>
      <img class="landscape b-lazy" src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== data-src="<?php echo $image->crop(300,180)->url() ?>" alt="<?php echo $project->title()->html() ?>" >

   <?php endif ?>

	<h5><?php echo $project->title()->html() ?></h5>
 </a>

<ul>
  <?php foreach($tags as $tag): ?>
<li>
  <a href="<?php echo url('projekte/tag:' . urlencode($tag)) ?>"><?php echo $tag ?>
  </a>
</li>
  <?php endforeach ?>
</ul>

 </div>

  <?php endforeach ?>

Yes, of course, you can use if statements in a foreach loop and vice versa depending on your needs.

There’s a problem in your code, though. You use the $project variable at the top of the code, but it is only defined later in your foreach loop. You will have to move that line somewhere after the first foreach loop

<?php foreach(page('projekte')->children()->visible()->sortBy('year', 'desc') as $project): ?>

<div class="grid-item">
  <a href="<?php echo $project->url() ?>">
<?php if($image = $project->images()->sortBy('orientation', 'asc')->first()): ?>
      <img class="landscape b-lazy" src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== data-src="<?php echo $image->crop(300,180)->url() ?>" alt="<?php echo $project->title()->html() ?>" >

   <?php endif ?>

	<h5><?php echo $project->title()->html() ?></h5>
 </a>

<?php 
$tags = $project->tags()->split();
if(!empty($tags)):
?>
  <ul>
     <?php foreach($tags as $tag): ?>
       <li>
        <a href="<?php echo url('projekte/tag:' . urlencode($tag)) ?>"><?php echo $tag ?>
        </a>
      </li>
    <?php endforeach ?>
  </ul>
<?php endif ?>
 </div>

  <?php endforeach ?>

Oh ok, I updated the code-

what I actually was trying to ask is how to implement this line from the cookbook:

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

into my foreach loop. So that the loop shows all projects, except there are tags inside the URL (then only show the ones with this specific tag) - am I thinking this the right way? Or do I need the controller here?

My controller looks like this: there must be something wrong because I actually don’t even understand what the controller is about and when I click on a tag it results in an error-page :frowning:

<?php
  return function($site, $pages, $page) {

  	$projects = page('projekte')->children()->visible()->sortBy('date')->flip();

    if($tag = param('tag')) {
    	$projects = $projects->filterBy('tags', '=', urldecode($tag), ',');
    }
   
    return compact('projekte');

};

If you end up on an error page when you click on a tag, then the first thing you should do is check your links in developer tools.

There’s another error, your operator in filterBy(), should be

$projects = $projects->filterBy('tags', '==', urldecode($tag), ',');
```

(you can also leave that out, as it is the default anyway):

Ok I got it working!

I changed the tag link from

<a href="<?php echo url('projekte/tag:' . urlencode($tag)) ?>"><?php echo $tag ?></a>

to

<a href="<?php echo url('projekte/' . url::paramsToString(['tag' => $tag])) ?>"><?php echo $tag ?></a>

additionally:

As I used the projects overview as a snippet both on homepage and on projects page, I need to have two controllers with the same content for home-template and for projects-template.

Thanks for your help!