This is a split-off to my previous post /forum/themes-styling/hyphenise-tags-which-contain-spaces-t5003 in which I needed to hyphenize tags containing a space.
Basically I need to do the same, but now per page in a collection.
Is it possible to do this with tags of my children pages? I played with your solution but could not find out how to do, must be missing something.
The header of my parent page (where I want to use filters) is:
{# Step 1: Hyphenise each tag and add to array of tags #}{%forchildinpage.children%}{%fortaginchild.taxonomy.tag%}{%sethyphenisedTag=tag|hyphenize%}{%settags=tags|merge([hyphenisedTag])%}{%endfor%}{%endfor%}{# Step 2: Join array of hyphenised tags #}{%setfilter=tags|join('')%}<lidata-filter="{{filter}}"></li>
But now I have the following critey error on the page rendering:
TXT
Twig \ Error \ RuntimeError
The merge filter only works with arrays or "Traversable", got "NULL" as first argument in "@Page:F:/wamp64/www/maconnerie-rousseau-grav/user/pages/02.realisations" at line 60.
Very sorry, I think I forgot to add {% set tags = [] %}. I tried so many times yesterday that I don't remember if I used it in my first tries. I try again.
Just to clarify, as I am calling my child pages taxonomy with, I guess I don't need to use or modify this, it was just for the example?
Yes, to follow your example, I created back the tag called "tag" in my site.yaml file.
So I did this and it displays all my child pages tags (using a <p> to display the output):
TWIG
{%settags=[]%}{# Step 1: Hyphenise each tag and add to array of tags #}{%forchildinpage.children%}{%fortaginchild.taxonomy.tag%}{%sethyphenisedTag=tag|hyphenize%}{%settags=tags|merge([hyphenisedTag])%}{%endfor%}{%endfor%}{# Step 2: Join array of hyphenised tags #}{%setfilter=tags|join('')%}<p>{{filter}}</p>
I would like to iterate my child pages and display only the tags which have been chosen for them. So I tried {% for p in page.collection %}:
TWIG
{%forpinpage.collection%}{%settags=[]%}{# Step 1: Hyphenise each tag and add to array of tags #}{%forchildinpage.children%}{%fortaginchild.taxonomy.tag%}{%sethyphenisedTag=tag|hyphenize%}{%settags=tags|merge([hyphenisedTag])%}{%endfor%}{%endfor%}{# Step 2: Join array of hyphenised tags #}{%setfilter=tags|join('')%}<p>{{filter}}</p>{%endfor%}
But it outputs all the values of "tag" instead of only the ones selected for the child page:
Below the iteration of my two existing child pages
That's because you're looping through {% for child in page.children %} no matter which page you're on. You should change that to {% for child in p.children %}, because you're inside another loop now and single item is p. But I guess p might not have children in this case 😕
What's your page structure, on which page you're looping and what end result you want exactly to be?
// Twig
{# For each 'child' of current 'page' #}{%forchildinpage.children%}{%settags=[]%}{# Step 1: Hyphenise each tag of 'child' and add to array of tags #}{%fortaginchild.taxonomy.tag%}{%sethyphenisedTag=tag|hyphenize%}{%settags=tags|merge([hyphenisedTag])%}{%endfor%}{# Step 2: Join array of hyphenised tags #}{%setfilter=tags|join('')%}<lidata-filter="{{filter}}"></li>{%endfor%}
HTML
// Generated HTML
<lidata-filter="a b hyphenize-me c"></li><lidata-filter="x y hyphenize-me z"></li>