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

Displaying All Taxonomies that are set

Solved by Claud Rusnac View solution

Started by Claud Rusnac 8 years ago · 18 replies · 3300 views
8 years ago

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

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

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:

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

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

8 years ago

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.

TWIG
<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
8 years ago

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

8 years ago

I am getting an error:

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

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?

8 years ago

I get an error:

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

8 years ago

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) }}

8 years ago

Does this produce the output you want ?

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

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} [] []

8 years ago

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

20%20AM|690x151

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

25%20AM|690x279

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:

13%20AM|690x285

8 years ago

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

8 years ago

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.

8 years ago

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.

8 years ago

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.

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

Frontmatter:

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

HTH

8 years ago

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.

TWIG
{% 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).
last edited 03/02/18 by Claud Rusnac
8 years ago

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?

8 years ago Solution

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

TWIG
<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>

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 193 2 months ago
Themes & Styling · by Ian, 2 months ago
3 90 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 449 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 44 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 123 3 months ago