Deight
August 9, 2021, 8:06pm
1
I cant find solution for separation tags or categories, taxonomy list if i show page tags for example with this:
{% 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:
{% 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!
OleVik
August 10, 2021, 12:23am
2
|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.
@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:
{{ [1, 2, 3]|join }}
{# returns 123 #}
{{ [1, 2, 3]|join('|') }}
{# outputs 1|2|3 #}
{{ [1, 2, 3]|join(', ', ' and ') }}
{# outputs 1, 2 and 3 #}
Since page.taxonomy.tag
is an array, the following will solve your issue:
Replace:
{% if page.taxonomy.tag %}
{% for tag in page.taxonomy.tag %}
{{ tag|join(',') }}
{% endfor %}
{% endif %}
with:
{{ page.taxonomy.tag | join(', ') }}
Deight
August 10, 2021, 9:51am
4
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.
Deight
August 10, 2021, 9:52am
5
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!
@Deight , That’s because you keep using the for loop… For each tag on the page, you print the combined/joined tags.
Deight
August 11, 2021, 12:17pm
7
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 …
Deight
August 11, 2021, 12:28pm
8
I’m very sorry, just use the following instead of the whole call:
{{ page.taxonomy.tag | join(', ') }}