Pass a taxonomy name by variable into a generic twig partial


#1

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:

{% 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:

 {% 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:

{% 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


#2

Welcome to Grav @aSiteMade4u! Not sure if this is quite what you are looking for, but here is an very similar example to use as a reference:

PS - While Quark is the current default theme for Grav, Antimatter was the previous one and can server as a great learning resource etc.


#3

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!