Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Pass a taxonomy name by variable into a generic twig partial

Started by Stephen S. 7 years ago · 2 replies · 1057 views
7 years ago

Hi all,
I just switched from WordPress+Timber (WP's port of twig) to Grav, which I find amazing.
I have some issues translating my knowledge of twig from Timber to Grav.

For example, I used to have a generic partial to deal with taxonomies in posts:

TWIG
{% if post.terms( taxo ) %}
    {{ sufx }}
{% endif %}
{% for term in post.terms( taxo ) %}
    {{term.name}}
    {% if loop.last %}
    {% else %}
        -
    {% endif %}
{% endfor %}

So that I could easily display the taxonomy in a post just by writing:

TWIG
 {% include 'partials/gen-taxo.twig' with {'taxo': 'my-taxonomy', 'sufx': 'my-suffix'} %}

I am trying to do the same in Grav. I expected this to work:

TWIG
{% if page.header.taxonomy(taxo) %}
    {{ sufx }}
{% endif %}
{% for term in page.header.taxonomy( taxo ) %}
    {{ term }}
    {% if loop.last %}
        {% else %}
        -
    {% endif %}
{% endfor %}

I know the variables taxo and suffx are well passed to the twig template because I am able to display them through the include.

But it does not.
Any help much appreciated.
Stephen

last edited 03/06/19 by Stephen S.
7 years ago

Thanks @paulhibbitts but not exactly.
In your example, you are passing the name of the taxonomy 'tag' while I am trying to pass a variable with the name of the taxonomy, so that my template is generic and displays any taxonomy the exact same way.
Thx!

last edited 03/06/19 by Stephen S.

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 198 2 months ago
Themes & Styling · by Ian, 2 months ago
3 94 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 455 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 48 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 128 3 months ago