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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

Taxonomy separator

Solved by Daniel Dobiášovský View solution

Started by Daniel Dobiášovský 5 years ago · 7 replies · 594 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

|join() works on the array itself, page.taxonomy.tag. If you are iterating with for, echo the separator directly like {{ tag }},. Optionally, use {% if not loop.last %}, {% endif %} to avoid the superfluous comma after the last item.

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

My problem was not in the separator at the end, but in the fact that they were not generated at all when using "join"

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 Jerry Hunt, 4 days ago
2 80 9 hours ago
General · by pamtbaau, 14 hours ago
1 51 13 hours ago
General · by Andy Miller, 1 day ago
0 44 1 day ago
General · by Marcel, 12 months ago
6 346 5 days ago
General · by Duc , 5 days ago
3 40 5 days ago