Displaying All Taxonomies that are set

I have some code that is displaying a list of the taxonomies, but I am trying to display the values as well. Below is my code. I would ultimately like a list that has the Taxonomy: Value

<ul>
    {% set taxlist = taxonomylist.get() %}
        {% for tax,value in taxlist %}
            {% if tax is not empty %}
                   <li>{{ tax|capitalize }}</li>
            {% endif %}
    {% endfor %}
   </ul>
1 Like

Hi
I’m not 100% sure what you’re looking for but I think your code will only output the actual taxonomy name(s). If, instead, you want a list of the taxonomy values then you’d want something like this:

{% set taxlist = taxonomylist.get() %}

{% if taxlist %}

<span class="tags">
    {% for tax,value in taxlist[taxonomy] %}

        <a href="{{ base_url }}/{{ taxonomy }}{{ config.system.param_sep }}{{ tax|e('url') }}">{{ tax }}</a>

    {% endfor %}
</span>
{% endif %}

If you’ve got multiple taxonomies and so want to show the value as well as the taxonomy that is belongs too would be pretty much the same as above but with extra output:

{% set taxlist = taxonomylist.get() %}

{% if taxlist %}

<span class="tags">
    {% for tax,value in taxlist[taxonomy] %}

        <a href="{{ base_url }}/{{ taxonomy }}{{ config.system.param_sep }}{{ tax|e('url') }}">{{ tax }}:{{ taxonomy }}</a>

    {% endfor %}
</span>
{% endif %}

Hopefully one of these helps

Thanks for the response, but this is not what I am looking for. I pretty much want to show (on every page) a list of the available taxonomies and their values.

My current code has me 1/2 way there. The current code outputs a list of all the taxonomies on a page, but not the values.

<ul>
{% set taxlist = taxonomylist.get() %}
  {% for tax,value in taxlist|sort %}
		{% if tax is not empty %}
			<li>{{ tax|capitalize }}</li>
		{% endif %}
  {% endfor %}
</ul>

Current Output:

  • Archives_year
  • Archives_month
  • Compliance
  • Category
  • Audience
  • Type
  • Tags

DESIRED OUTPUT:

  • Archives_year: 2018
  • Archives_month: 12
  • Compliance: Yes
  • Category: GDPR
  • Audience: Mgmt
  • Type: Standard
  • Tags: Policy, Standard

On my system the 2nd option I gave produced an output like your desired output, what did you get?

I am getting an error:

0 - An exception has been thrown during the rendering of a template ("Illegal offset type in isset or empty").

Did you try to dump the output of taxlist just to confirm there is actually data there before all the sorting and filtering start?
I suspect the issue is in this line: {% for tax,value in taxlist|sort %} as without pulling out the taxonomy here you won’t be able to then later use it in your output. Have you tried just swapping that line for this one: {% for tax,value in taxlist[taxonomy] %}? Any different?

I get an error:

0 - An exception has been thrown during the rendering of a template ("Illegal offset type in isset or empty").

What code exactly are you using to get that error?

I assumed, maybe incorrectly, your taxonomy list is actually called “taxonomy” is this correct?
Did you do a dump on taxlist on its own just to be sure there is data to be had? You would need to enable the debugger and also Twig debug then use it like this:
{% set taxlist = taxonomylist.get() %} {{ dump(taxlist) }}

Does this produce the output you want ?

<ul>
    {% set taxlist = taxonomylist.get() %}
        {% for tax,value in taxlist %}
            {% if tax is not empty %}
                   <li>{{ tax|capitalize }} : {{ value }}</li>
            {% endif %}
    {% endfor %}
   </ul>

This is the error I am getting:

[2018-02-23 17:21:54] grav.CRITICAL: An exception has been thrown during the rendering of a template ("Illegal offset type in isset or empty"). - Trace: #0 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(403): Twig_Template->displayWithErrorHandling(Array, Array) #1 /nisp-cms/vendor/twig/twig/lib/Twig/Environment.php(467) : eval()'d code(98): Twig_Template->display(Array) #2 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(432): __TwigTemplate_87f269f14f64e392c614467ff72ec5f9d0a04fc2d8bceb7e56e158e1a1ddcea1->doDisplay(Array, Array) #3 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(403): Twig_Template->displayWithErrorHandling(Array, Array) #4 /nisp-cms/vendor/twig/twig/lib/Twig/Environment.php(467) : eval()'d code(143): Twig_Template->display(Array) #5 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(215): __TwigTemplate_18e4cfdceca5b359d50b4eb88050ca7146c8a3e4cc535ff5cc7115e1c6f4367c_1793451404->block_content(Array, Array) #6 /nisp-cms/vendor/twig/twig/lib/Twig/Environment.php(467) : eval()'d code(429): Twig_Template->displayBlock('content', Array, Array) #7 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(215): __TwigTemplate_a270c09822c6dcb2d44aebc0913693a3c45eb0ea76497357e3953a883ffba622->block_body(Array, Array) #8 /nisp-cms/vendor/twig/twig/lib/Twig/Environment.php(467) : eval()'d code(241): Twig_Template->displayBlock('body', Array, Array) #9 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(432): __TwigTemplate_a270c09822c6dcb2d44aebc0913693a3c45eb0ea76497357e3953a883ffba622->doDisplay(Array, Array) #10 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(403): Twig_Template->displayWithErrorHandling(Array, Array) #11 /nisp-cms/vendor/twig/twig/lib/Twig/Environment.php(467) : eval()'d code(115): Twig_Template->display(Array, Array) #12 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(432): __TwigTemplate_18e4cfdceca5b359d50b4eb88050ca7146c8a3e4cc535ff5cc7115e1c6f4367c_1793451404->doDisplay(Array, Array) #13 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(403): Twig_Template->displayWithErrorHandling(Array, Array) #14 /nisp-cms/vendor/twig/twig/lib/Twig/Environment.php(467) : eval()'d code(19): Twig_Template->display(Array) #15 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(432): __TwigTemplate_18e4cfdceca5b359d50b4eb88050ca7146c8a3e4cc535ff5cc7115e1c6f4367c->doDisplay(Array, Array) #16 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(403): Twig_Template->displayWithErrorHandling(Array, Array) #17 /nisp-cms/vendor/twig/twig/lib/Twig/Template.php(411): Twig_Template->display(Array) #18 /nisp-cms/vendor/twig/twig/lib/Twig/Environment.php(363): Twig_Template->render(Array) #19 /nisp-cms/system/src/Grav/Common/Twig/Twig.php(349): Twig_Environment->render('filter.html.twi...', Array) #20 /nisp-cms/system/src/Grav/Common/Service/OutputServiceProvider.php(27): Grav\Common\Twig\Twig->processSite('html') #21 /nisp-cms/vendor/pimple/pimple/src/Pimple/Container.php(118): Grav\Common\Service\OutputServiceProvider->Grav\Common\Service\{closure}(Object(Grav\Common\Grav)) #22 /nisp-cms/system/src/Grav/Common/Processors/RenderProcessor.php(19): Pimple\Container->offsetGet('output') #23 /nisp-cms/system/src/Grav/Common/Grav.php(132): Grav\Common\Processors\RenderProcessor->process() #24 /nisp-cms/system/src/Grav/Common/Grav.php(379): Grav\Common\Grav->Grav\Common\{closure}() #25 [internal function]: Grav\Common\Grav::Grav\Common\{closure}('render', 'Render', Object(Closure)) #26 /nisp-cms/system/src/Grav/Common/Grav.php(355): call_user_func_array(Object(Closure), Array) #27 /nisp-cms/system/src/Grav/Common/Grav.php(133): Grav\Common\Grav->__call('measureTime', Array) #28 /nisp-cms/index.php(52): Grav\Common\Grav->process() #29 {main} [] []

I am able to dump this {% set taxlist = taxonomylist.get() %} {{ dump(taxlist) }}

I am able to output the Taxonomy list, but not the values.

When I add {{ value }} to <li>{{ tax|capitalize }}}</li> I get an error:

0 - An exception has been thrown during the rendering of a template ("Array to string conversion").

Here is a sample of my pages:

<ul>
{% set taxlist = taxonomylist.get() %}
    {% for tax,value in taxlist %}
        {% if tax is not empty %}
               <li>{{ tax|capitalize }} : {{ value }}</li>
        {% endif %}
{% endfor %}

Have you tried swapping this line {% for tax,value in taxlist %} for {% for tax,value in taxlist[taxonomy] %}? I still think that’s the route cause of your errors.

Yes - when I use {% for tax,value in taxlist[taxonomy] %} I get the error. When I use {% for tax,value in taxlist %} I don’t get an error, only the Taxonomies are displayed.

I Issue is that I can’t get the values to appear.

Actually I think this plugin is what you are looking for, I haven’t used it myself but it looks to be made to handle multiple taxonomies like you’ve got so no need to reinvent the wheel.

https://github.com/ash0080/grav-plugin-cascade-filters

Hi
I don’t know if that plugin worked for your needs or not but if you’re still looking for some code I have just done something a bit similar from what you were looking for I think. My data has a different layout so might need some tweaking.

{% set taxlist = taxonomylist.get() %}
{% for tax,value in taxlist %}
    {% set t = tax %}
    {% for v in value|keys %}
        <p>{{ t }}: {{ v }}</p>
    {% endfor %}
{% endfor %}

Frontmatter:

taxonomy:
    category:
        - blog
    tag:
        - News
        - Visual
        - Text
    blog:
        - 'blog 1'
        - 'blog 2'

HTH

Thanks for the code, but this is displaying all the taxonomies for all pages. I am looking to display the taxonomies that are applicable per page. I am currently using the following code to display the tags.

{% if page.taxonomy.tags is not empty %}
		<tr>
			<td><strong>Tags</strong></td>
			<td>{% for tag in page.taxonomy.tags %}{{ tag|upper|replace('-', ' ') }}{% if not loop.last %},{% endif %} {% endfor %}</td>
		</tr>
	{% endif %}

The problem with the code above is that the taxonomy is hardcoded and if a new taxonomy is added, I need to add a new if statement.

My ideal code would:

  • Loop through all the taxonomies that are set on each page
  • If a taxonomy exists, display the taxonomy and the values (in a comma separated list).

I don’t know page.taxonomy but if its the same array structure then you should just able to swap
{% set taxlist = taxonomylist.get() %}
for
{% set taxlist = page.taxonomy %}
That any better or just kills the page?

I figured this out by using the above code with some minor adjustments:

<table>
    <thead>
        <tr>
           <th>Taxonomy</th>
           <th>Value(s)</th>
         </tr>
    </thead>
	<tbody>
    
  {% set taxlist = page.taxonomy %}        
      {% for tax,value in taxlist|sort %}
            {% if value is not empty %}
                <tr>
                    <td valign="bottom">
                        <strong>{{ tax|capitalize }}</strong>
                    </td>
                    <td valign="bottom">
                        {% for v in value %}
                            {{ v|upper|replace('-', ' ') }}{% if not loop.last %},{% endif %}
                        {% endfor %}
                    </td>                
                </tr>
            {% endif %}

    {% endfor %}

    </tbody>
</table>