Tags with spaces


#1

Hey guys,

I’m trying to use tags with spaces in my site (e.g. “gordon freeman”). What kirby or my used template (Baltrum) generates is an URL like: /tag:gordon+freeman. Like this, of course, kirby doesn’t find any post, only if it has two tags “gordon” and “freeman”.

Any suggestions to this?

Thanks in advance!
Matti


#2

Use urlencode() and urldecode(), e.g.

urldecode(param('tag'))

URL problem with tag filtering
#3

Thanks for your quick answer!

urlencode() and urldecode() is already used. See https://github.com/getkirby/plugins/blob/master/tagcloud/tagcloud.php.

Any further suggestions?


#4

Try rawurlencode() and rawurldecode(), then …


#5

Thanks again! But this also didn’t work.

Now the system gives me a link like site.tld/tag:gordon freeman
And on the result page it says “Posts tagged with ”gordon%20freeman“”, but still no results.

I guess it must be the built in URL interpreter of kirby.

Any further suggestions?


#6

Could you post your code? I have been using tags with spaces without any problems in the past using urlencode() and urldecode() and with the tagcloud plugin that should work automatically anyway.


#7

Thanks for the info that tags with spaces usually work in kirby!

That brought me to looking into the template files of my applied theme. There the author forgot to use urldecode(). I just added it and now it works fine:

/* Filter by tag, if needed */
if(param('tag', false)) {
    $posts = $posts->filterBy('tags', urldecode(param('tag')), ',');
}

Thanks again for your help!


Output a menu based on child page tags
#8

I am running into a similar issue. I have implemented urldecode and urlencode.

For example, for a category called Alpha Beta the encoded url is:

http://localhost/site/category:Alpha+Beta

When I click on that link, the browser loads the address

http://localhost/site/category:Alpha%20Beta

And at this point I get a generic browser window error saying:

This page isn’t working
localhost redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS

My controller code is:

return function($site, $pages, $page) {

  $collection = page('collection')->children()->visible();

  if ($tag = urldecode(param('category'))) {
    $collection = $collection->filterBy('tag', $tag, ',');
  }

  return compact('collection', 'tag');

};

The code that generate the link in my nav is:

<div class="nav">
  <ul>
    <?php foreach($tags as $tag): ?>
      <li>
        <a class="nav__link" href="<?= url('category:' . urlencode($tag['name']))?>">
          <?php echo $tag['name'] ?>
        </a>
      </li>
    <?php endforeach ?>
  </ul>
</div>

#9

Have you tried with rawurlencode() and rawurldecode() instead?


#10

I didn’t try rawurlencode().
I have instead create a new clone of a fresh kirby starterkit and tested that categories with spaces do work correctly.
After I rewrote the new installation using my old code. So far it’s working. It must have been some other error that was triggering ERR_TOO_MANY_REDIRECTS.