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.

General

How do i select all tags in my twig template in grav?

Started by gautam 8 years ago · 4 replies · 1548 views
8 years ago

have the following code in my default.html.twig file to display all the titles of the blog pages with the tag : dog:

TWIG
<ul>
        {% for post in taxonomy.findTaxonomy({'tag':'dog'}) %}
        <li>{{ post.title }}<span>(0)</span></li>
        {% endfor %}
    </ul>

What i would really like to do is find all the tags and not just the dog tag, so i did the following:

TWIG
<ul>
        {% for post in taxonomy.findTaxonomy({'tag':'all'}) %}
        <li>{{ post.title }}<span>(0)</span></li>
        {% endfor %}
    </ul>

I changed the dog to all , but this does't really give me any results. I checked the documentation here for findTaxonomy , but it does't help much , also in the <span></span> inside the <li> , I would like to show how many articles for this perticular tag have been posted , eg. if there are five articles for the tag popcorn the html spitedd out should be:

<li>popcorn <span>(5)</span></li>. How do i achieve this ?

8 years ago

what about not using taxonomy but a generic collection something like:

TWIG
{% for post in page.collection( items: {'@page.descendants': '/blog'} ) %}

Some blogs have a collection defined in header, if that your case:

TWIG
{% for post in page.find('/blog-path').collection() %}

https://learn.getgrav.org/content/collections

For your other question, to show the number of posts, just get the page collection lenght something like:

TWIG
{{ page.find('/blog-path').collection()|length }}

or

TWIG
<span>( {{taxonomy.findTaxonomy({'tag':'dog'})|length }} )</span>

https://twig.symfony.com/doc/2.x/filters/length.html

You can play with all that, if you can hardcode your tags that may work. but if you need a more dynamic code, you will need to check how this plugin is made:
https://github.com/getgrav/grav-plugin-taxonomylist

👍 1
8 years ago

Thanks for that @hugoaf , what exactly is \blog-path here though ? is it something set in the header of the main blog page ? can you show me an example of how blog-path would look ?

8 years ago

page.find('path/to-page')

wil find the page:

TXT
`your-website.com/path/to-page`
👍 1
7 years ago

Don't want to necro but the actual solution is :

TWIG
<ul>
{% for animal in taxonomy.getTaxonomyItemKeys('tag') %}
    <li>{{ animal }}</li>
{% endfor %}
</ul>

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 3 days ago
2 70 5 hours ago
General · by pamtbaau, 10 hours ago
1 45 10 hours ago
General · by Andy Miller, 22 hours ago
0 43 22 hours ago
General · by Marcel, 12 months ago
6 341 4 days ago
General · by Duc , 5 days ago
3 35 5 days ago