You’d have to repeat the first step for each child page. Something like
{% for child in page.children %}
{% for tag in child.taxonomy.tag %}
{% set hyphenisedTag = tag|hyphenize %}
{% set tags = tags|merge([hyphenisedTag]) %}
{% endfor %}
{% endfor %}
{# Step 1: Hyphenise each tag and add to array of tags #}
{% for child in page.children %}
{% for tag in child.taxonomy.tag %}
{% set hyphenisedTag = tag|hyphenize %}
{% set tags = tags|merge([hyphenisedTag]) %}
{% endfor %}
{% endfor %}
{# Step 2: Join array of hyphenised tags #}
{% set filter = tags|join(' ') %}
<li data-filter="{{ filter }}"></li>
But now I have the following critey error on the page rendering:
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):
{% set tags = [] %}
{# Step 1: Hyphenise each tag and add to array of tags #}
{% for child in page.children %}
{% for tag in child.taxonomy.tag %}
{% set hyphenisedTag = tag|hyphenize %}
{% set tags = tags|merge([hyphenisedTag]) %}
{% endfor %}
{% endfor %}
{# Step 2: Join array of hyphenised tags #}
{% set filter = 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 %}:
{% for p in page.collection %}
{% set tags = [] %}
{# Step 1: Hyphenise each tag and add to array of tags #}
{% for child in page.children %}
{% for tag in child.taxonomy.tag %}
{% set hyphenisedTag = tag|hyphenize %}
{% set tags = tags|merge([hyphenisedTag]) %}
{% endfor %}
{% endfor %}
{# Step 2: Join array of hyphenised tags #}
{% set filter = 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?
// child2: default.md
taxonomy:
tag: [x, y, hyphenize me, z]
// Twig
{# For each 'child' of current 'page' #}
{% for child in page.children %}
{% set tags = [] %}
{# Step 1: Hyphenise each tag of 'child' and add to array of tags #}
{% for tag in child.taxonomy.tag %}
{% set hyphenisedTag = tag|hyphenize %}
{% set tags = tags|merge([hyphenisedTag]) %}
{% endfor %}
{# Step 2: Join array of hyphenised tags #}
{% set filter = tags|join(' ') %}
<li data-filter="{{ filter }}"></li>
{% endfor %}
// Generated HTML
<li data-filter="a b hyphenize-me c"></li>
<li data-filter="x y hyphenize-me z"></li>