This is a split-off to my previous post Hyphenise tags which contain spaces - #3 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:
---
title: Réalisations
process:
twig: true
markdown: true
twig_first: true
content:
items: '@self.children'
order:
by: date
dir: desc
limit: 20
pagination: false
---
The header of one of my children page is:
---
title: 'Test Réalisation'
date: '01-01-2018 00:00'
taxonomy:
chantier:
- Rénovation
prestation:
- Sols
- Placo
- Peinture
- Cuisine
- 'Salle de bain'
---
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 %}
Hello @Karmalakas, many thanks for your reply.
So following your advice, I tried to do this:
Karmalakas
{# 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.
Am I missing something?
Did you discard what @pamtbaau wrote?
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?
// default.md
taxonomy:
tag: [a, b, hyphenate me, c]
Assuming your page children have taxonomy.tag
defined, you should be fine
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
sols placo salle-de-bain peinture salle-de-bain sols placo peinture
sols placo salle-de-bain peinture salle-de-bain sols placo peinture
How could I display only the tags assigned to my child page for each child page?
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?
@brigaill, To extend on my previous answer which was for a single page, the following is per page in a collection of pages.
// child1: default.md
taxonomy:
tag: [a, b, hyphenize me, c]
// 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>
Hello @pamtbaau ,
Awesome, now it works perfectly, I’m so happy, thanks so much to you and @Karmalakas for your help!