Skip to content
Grav 2.0 is officially stable. Read the announcement →
General

Taxonomy separator

Solved by Daniel Dobiášovský View solution

Started by Daniel Dobiášovský 5 years ago · 7 replies · 731 views
5 years ago

I cant find solution for separation tags or categories, taxonomy list if i show page tags for example with this:

TWIG
{% if page.taxonomy.tag %}
    {% for tag in page.taxonomy.tag %}
    {{ tag }}
    {% endfor %}
{% endif %}

And i have tag like this:

Alfa Beta Gama

I want put the separator for this:

Alfa, Beta, Gama

I find solution with twig "join" but this is not work i need this:

TWIG
{% if page.taxonomy.tag %}
    {% for tag in page.taxonomy.tag %}
    {{ tag|join(',') }}
    {% endfor %}
{% endif %}

Knows this someone? I try to find in docs and everything must work but somewhare is problem and i dont know whare...

Thank you guys!

last edited 08/09/21 by Daniel Dobiášovský
5 years ago

@Deight, Please have a look at the Twig documentation about join to get a basic understanding of how the filter works.

Here are some examples from the Twig docs:

TXT
{{ [1, 2, 3]|join }}
{# returns 123 #}
TXT
{{ [1, 2, 3]|join('|') }}
{# outputs 1|2|3 #}
TXT
{{ [1, 2, 3]|join(', ', ' and ') }}
{# outputs 1, 2 and 3 #}

Since page.taxonomy.tag is an array, the following will solve your issue:

Replace:

TWIG
{% if page.taxonomy.tag %}
    {% for tag in page.taxonomy.tag %}
    {{ tag|join(',') }}
    {% endfor %}
{% endif %}

with:

TWIG
{{ page.taxonomy.tag | join(', ') }}
last edited 08/11/21 by pamtbaau
5 years ago

With this modification of the variable, the separator is added, but I don't know why the tags were generated 3 times in a row. Do you know why?

"tag1,tag2,tag3 tag1,tag2,tag3 tag1,tag2,tag3"

Github is close, thank you for your time.

5 years ago

@Deight, That's because you keep using the for loop... For each tag on the page, you print the combined/joined tags.

last edited 08/10/21 by pamtbaau
5 years ago

I can't solve this if a page has one tag it shows one tag, if it has two both it appears twice if three so three times etc ...

So should I use a call other than "for"?

{% if page.taxonomy.tag %}
{% for tag in page.taxonomy.tag %}
{{ page.taxonomy.tag | join(', ') }}
{% endfor %}
{% endif %}

This solution still cycles my tags.

I'm stupid, sorry ...

5 years ago Solution

I'm very sorry, just use the following instead of the whole call:

{{ page.taxonomy.tag | join(', ') }}

Suggested topics

Topic Participants Replies Views Activity
General · by jean-louis67, 3 weeks ago
2 308 3 weeks ago
General · by Andy Miller, 3 weeks ago
0 310 3 weeks ago
General · by Veehem, 4 weeks ago
0 263 4 weeks ago
General · by milkboy, 4 weeks ago
0 264 4 weeks ago
General · by ian russell, 1 month ago
2 335 1 month ago