Using Tagger and displaying the current tag selected

What I want is to display the current tag selected on the target resource. So the page would show “Current Category is [[+tag]]” or something.

I tried messing with TaggerGetCurrentTag , no luck there yet.

Any ideas?

I don’t use tagger, but if the tag is in a TV, you should be able to do this for the current page:

[[*tvName]]

Here is more what I mean.

One way around this might be to have something that gets the last word of the URI in the example above it would be /posts-by-tag/tag-2 So I just need to grab tag-2 , strip the hyphens and capitalize the first letter.

This is a horrible hack IMHO, but here is what I’m doing.

I made a snippet called getLastWordUrl

<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";   
echo basename($url);

Then dropped it into my content like as
[[getLastWordUrl:ucwords:replace=-== ]]

I’m replacing dashes with spaces and upper casing the first letter of each word.

Whatever works. :wink:

FYI, putting the conversions in the snippet will make it a whole lot faster. Chained output modifiers require parsing as an extra step.

<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";   
$url = basename($url);
$url = str_replace('-', ' ', $url);
return ucwords($url);

or as one line:

return ucwords(str_replace('-', ' ', basename("http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]")));

I was struggling with the same problem, particularily because the above method does not work well with umlauts in the alias: they get replaced automatically in Tagger, so you cannot use them as labels easily. But I found a solution to display the real tag name:

[[TaggerGetTags? &target=`7` &groups=`1` &rowTpl=`TagNameOnlyTpl`]]

For TagNameOnlyTpl you use:

[[If? 
  &subject=`[[!#get.kategorie]]`
  &operator=`EQ`
  &operand=`[[+alias]]`
  &then=`[[+tag]]`
]]

(kategorie is the name of your Tagger group. The [[!#get.xxx]] command to easily retrieve the value of xxx from the $_GET variable is part of the fastfield package. If is part of the If package)

This goes through all the tags and only displays its name only if the corresponding alias matches the one from the $_GET.

You can use something similar for a list of clickable links to all the used tags on the top of your overview page (7). The clicked tag will get the css-class active, so you can highlight that one:

[[TaggerGetTags? &target=`7` &groups=`1`&rowTpl=`TaglisteTpl`]]

with: TagListeTpl:

<li>
  <a class="[[If? 
  &subject=`[[!#get.kategorie]]`
  &operator=`EQ`
  &operand=`[[+alias]]`
  &then=`active`
]]" href="[[+uri]]">[[+tag]]</a>
</li>

Hope that helps!

As the original poster noted, there is a snippet TaggerGetCurrentTag for this purpose.
With a call like this

[[!TaggerGetCurrentTag? &groupTpl=`@INLINE [[+tags]]` &tagTpl=`@INLINE [[+tag]]` &tagSeparator=`, ` ]]

you should get the same result.


The snippet TaggerGetTags already provides a placeholder active. So the same could be achieved with this call:

[[!TaggerGetTags? &groups=`1` &rowTpl=`TaglisteTpl`]]

Chunk TaglisteTpl:

<li>
  <a class="[[+active:is=`1`:then=`active`]]" href="[[+uri]]">[[+tag]]</a>
</li>

Make sure to call the snippets uncached. These solutions also works, if you select multiple tags (https://www.domain.com/resource?kategorie=tag1,tag2).

5 Likes