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

Find and print categories in twig file

Started by Muut Archive 11 years ago · 8 replies · 1378 views
11 years ago

I have edited the default home page (/user/pages/01.home) and changed the page file into "Modular".

Under the home page, I've created a new modular page called "Categories" (/user/pages/01.home/_categories).

Under the categories page, I've added another modular page, where each modular page has a taxonomy like "Term 1", "Term 2", "Term 3", "Term 4" and so on.

I've created a *.html.twig to print those content.

Now, how can I find the categories used and group them based from their taxonomy terms? Also, I would like to add the category itself as a title on each group.

This is what I've got so far (which I found in the documentation), and I've wanted to find the categories dynamically instead of typing them one by one just to find and print the taxonomy associated with it.

TWIG

{% for post in taxonomy.findTaxonomy({'category':['Term 1']}) %}
  {{ post.title }}
  {{ post.content }}
{% endfor %}

{% for post in taxonomy.findTaxonomy({'category':['Term 2']}) %}
  {{ post.title }}
  {{ post.content }} 
{% endfor %}

{% for post in taxonomy.findTaxonomy({'category':['Term 3']}) %}
  {{ post.title }}
  {{ post.content }}
{% endfor %}

{% for post in taxonomy.findTaxonomy({'category':['Term 4']}) %}
  {{ post.title }}
  {{ post.content }}
{% endfor %}
---
👍 1
11 years ago

You mean if you had a page with frontmatter like:

YAML
title: My Page 1
taxonomy:
   category: ['Term 1']

and another page with:

YAML
title: My Page 2
taxonomy:
   category: ['Term 2']

This Twig template could be shared between them:

TWIG
{% for post in taxonomy.findTaxonomy({'category': page.taxonomy.category}) %} 
  {{ post.title }}
  {{ post.content }}
{% endfor %}

Hopefully i'm not misunderstanding your question...

11 years ago

Thank you for your quick response rhukster. Yep, that is what in my frontmatter. When I've copied and pasted the code you've provided, there are no content printed. The output is "null" when I tried to debug the "page.taxonomy.category".

11 years ago

I forgot to mention that my template for the categories is located in my template partials and just included that file in my base.html.twig.

11 years ago

Maybe you can zip up what you have and put it on dropbox for me. PM me the link and I can see what your actually trying to do :)

11 years ago

Hi rhukster, kindly check your inbox. Thank you.

11 years ago

This should give you an idea.. try this

TWIG

<div class="menu-items">
    <div class="container">
         {% set terms = [] %}
         {% for post in page.find('/home/_03menu').children %}
           {% for term in post.taxonomy.category %}
             {% if term not in terms %}
               {% set terms = terms|merge([term]) %}
             {% endif %}
           {% endfor %}   
         {% endfor %}

        {% for term in terms %}
          <div class="col-lg-3">
            <h2> {{ term }} </h2>
            {% for p in taxonomy.findTaxonomy({'category' : term}) %}
              <div class="item row">
                <h3> {{ p.header.title }} </h3>
                <p> {{ p.content }} </p>
              </div>
            {% endfor %} 
          </div>
        {% endfor %}

    </div>
</div>
---
11 years ago

Thank you danreb. The code works well :)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1350 9 years ago
Archive · by Muut Archive, 9 years ago
2 935 9 years ago
Archive · by Muut Archive, 9 years ago
2 4061 9 years ago
Archive · by Muut Archive, 9 years ago
1 2948 9 years ago
Archive · by Muut Archive, 9 years ago
3 1119 9 years ago