---
title: '[SOLVED] Filter by template type'
url: 'https://getgrav.org/forum/themes-styling/solved-filter-by-template-type-t2597'
markdown: 'https://getgrav.org/forum/themes-styling/solved-filter-by-template-type-t2597.md'
date: '2026-06-21'
description: "Hi, I know this has been asked before but I could not get any solution working and since I'm a newbie Grav user, I'm probably missing something very obvious. I"
---

[Themes & Styling](https://getgrav.org/forum/themes-styling)

#   \[SOLVED\] Filter by template type 

 Solved by Christian [View solution](https://getgrav.org/forum/themes-styling/solved-filter-by-template-type-t2597#post-21176)

  Started by Christian 9 years ago · 3 replies · 1418 views    

 [9 years ago](https://getgrav.org/forum/themes-styling/solved-filter-by-template-type-t2597#post-21150 "Permalink") 

Hi,
I know this has been asked before but I could not get any solution working and since I'm a newbie Grav user, I'm probably missing something very obvious.

I want to make the haywire home.twig file display the latest three blog posts, but not only from /blog (as in the original theme) but from all blogs that I have on the page.

```
ROOT
|-blog1
|    |-post1
|    '-post2
'-blog2
    |-post1
    '-post2
```

I have tried the following (copied from the top of the home.html.twig file)

```twig
{% extends 'partials/base.html.twig' %}
{% block content %}
{% set all_blogs = page.collection({
        'items': '@root.descendants',
        'order': {
            'by': 'date',
            'dir': 'desc'
        }
    }).ofType('blog')
%}
```

Then trying to use

`{% for post in all_blogs.children.slice(0, 3) %}`

Which yields no entry at all.

Also, trying any selection like

`{% for post in pages.parent.template('blog').children.slice(0, 3) %}`

does not work. Nor does

`{% for post in page({'items':{'@taxonomy':{'template': 'blog'}}}).children.slice(0,3) %}`

All these snippets were tried in the aforementioned twig file. Is that the correct place?

Also, my debug bar does not work, so I am struggling on multiple ends 🤔

Any help is highly appreciated.

Kind Regards,
Christian

 [9 years ago](https://getgrav.org/forum/themes-styling/solved-filter-by-template-type-t2597#post-21172 "Permalink") 

Hi
I don't know why none of these attempts work but I'd suggest starting smaller with your loop to try and narrow down the issue such as starting with `{% for post in all_blogs %}` and just confirm at least *something* is filtering. There's a lot to assume here so we'd probably need more code such as what's in your loop to be able to suggest more.

The debugger is a great help - make sure you have "Twig Debugging" and also the "Debugger" both enabled to use it - but if its not working you can also just output a field directly onto your page so instead of `{{ dump(post) }}` you can just do something like `<p>post test: {{ post }}</p>` to see the value instead.

HTH

 [9 years ago](https://getgrav.org/forum/themes-styling/solved-filter-by-template-type-t2597#post-21176 "Permalink")  Solution 

Thanks for your help!

Ok, first I needed to enable the debug bar (which was rather complicated as I needed to add debug bar support to the theme I am using).

So this now works:

```twig

{% set allblogitems = page.collection({
    'items': '@root.descendants',
    'order': {
        'by': 'date',
        'dir': 'desc',
    },
}).ofType('item') %}

{% for post in allblogitems if post.parent.template=='blog' %}
{{ post.template() }}
{% endfor %}
```

The only thing I want to do now, is to restrict the output to 3 items. As of now, all items where the template type is 'item' and the parent template is 'blog' are shown.

I know I could do this with an if conditional like so:

```twig
{% if loop.index0 < 3 %}
{% endif %}
```

What I would really love is to select all items where the parent is a blog directly in the collection, that way I could use slice.

 [9 years ago](https://getgrav.org/forum/themes-styling/solved-filter-by-template-type-t2597#post-21177 "Permalink") 

It might be possible to it like this
`{% for post in allblogitems if post.parent.template=='blog'|slice(0, 3) %}`
but I don't think you can slice on a condition like that in a loop so that if statement might be the best way to go.

I don't know if you can do this with pages but possibly something like this might also work?

```twig
{% set allblogitems = page.collection({
    'items': '@page.children': [/blog, /blog2]
    'order': {
        'by': 'date',
        'dir': 'desc',
    },
    'limit': 3
}) %}
```

[Log in](https://getgrav.org/login?return=%2Fforum%2Fthemes-styling%2Fsolved-filter-by-template-type-t2597) to reply.

###  Suggested topics

 | Topic | Participants | Replies | Views | Activity |
|---|---|---|---|---|
| [Modular Page Background Image Display](https://getgrav.org/forum/themes-styling/modular-page-background-image-display-t9486)    [Themes & Styling](https://getgrav.org/forum/themes-styling) · by TomW, 2 weeks ago |  | 2 | 200 | [2 days ago](https://getgrav.org/forum/themes-styling/modular-page-background-image-display-t9486 "by TomW") |
| [Inheriting a Theme does not quite work correctly](https://getgrav.org/forum/themes-styling/inheriting-a-theme-does-not-quite-work-correctly-t9501)    [Themes & Styling](https://getgrav.org/forum/themes-styling) · by themetman, 3 days ago |  | 0 | 48 | [3 days ago](https://getgrav.org/forum/themes-styling/inheriting-a-theme-does-not-quite-work-correctly-t9501 "by themetman") |
| [\[quark\] mobile menu as the modular page menu](https://getgrav.org/forum/themes-styling/quark-mobile-menu-as-the-modular-page-menu-t1132)    [Themes & Styling](https://getgrav.org/forum/themes-styling) · by Bruno, 4 years ago |  | 4 | 1321 | [3 days ago](https://getgrav.org/forum/themes-styling/quark-mobile-menu-as-the-modular-page-menu-t1132 "by AH") |
| [Grav2 with Quark2 – All pages in the hamburger-menu of a one-pager](https://getgrav.org/forum/themes-styling/grav2-with-quark2-all-pages-in-the-hamburger-menu-of-a-one-pager-t9485)    [Themes & Styling](https://getgrav.org/forum/themes-styling) · by AH, 2 weeks ago |  | 0 | 173 | [2 weeks ago](https://getgrav.org/forum/themes-styling/grav2-with-quark2-all-pages-in-the-hamburger-menu-of-a-one-pager-t9485 "by AH") |
| [Is it a way to get the "content - summary" in a twig template ?](https://getgrav.org/forum/themes-styling/is-it-a-way-to-get-the-content-summary-in-a-twig-template-t9431)    [Themes & Styling](https://getgrav.org/forum/themes-styling) · by FrViPofm, 2 months ago |  | 4 | 668 | [1 month ago](https://getgrav.org/forum/themes-styling/is-it-a-way-to-get-the-content-summary-in-a-twig-template-t9431 "by Paul Hibbitts") |
