Skip to content
● Meet Grav Guard: keep every Grav site you run secure and up to date, all from one place. Free for up to 3 sites. Read more →

Hyphenise tags which contain spaces

Solved by pamtbaau View solution

Started by Benjamin 6 years ago · 1 replies · 1229 views
6 years ago

Now I am trying to hyphenize tags which have spaces, but if I do this:

{% set filter = p.taxonomy.prestation|join(' ')|hyphenize %}{{ filter }}

it outputs hyphens even between tags as shown below:

data-filter="sols-placo-peinture-cuisine-salle-de-bain"

Is there a solution to have hyphens only for tags, as following?

data-filter="sols placo peinture cuisine salle-de-bain"

I could not find out how to apply |hyphenize to tags only.

6 years ago Solution

@brigaill, Try the following:

YAML
// default.md

taxonomy:
  tag: [a, b, hyphenate me, c]
TWIG
// Twig

{% set tags = [] %}

{# Step 1: Hyphenise each tag and add to array of tags #}
{% for tag in page.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>
HTML
// Generated HTML

<li data-filter="a b hyphenate-me c"></li>

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by MarkP, 2 weeks ago
3 193 2 weeks ago
Themes & Styling · by TomW, 2 weeks ago
0 111 2 weeks ago
Themes & Styling · by themetman, 3 weeks ago
1 206 3 weeks ago
Themes & Styling · by AH, 1 month ago
1 324 3 weeks ago
Themes & Styling · by Bruno, 4 years ago
6 1523 3 weeks ago