Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

Hyphenise tags which contain spaces

Solved by pamtbaau View solution

Started by Benjamin 6 years ago · 1 replies · 877 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 FrViPofm, 17 hours ago
1 31 4 hours ago
Themes & Styling · by stuart young, 1 month ago
3 480 1 month ago
Themes & Styling · by Sebadamus, 2 months ago
5 481 2 months ago
Themes & Styling · by martynfoster735, 2 months ago
1 460 2 months ago
Themes & Styling · by Justin Young, 2 months ago
1 490 2 months ago