isFirst() not working in page collection

I am trying to use grav’s isFirst() function to detect when the user is on the first page of a page collection and to display or hide a class name but this doesn’t seem to be working:

        <div class="moduleindexcompact{% if not page.isFirst %} hidden{% endif %}">

The “hidden” class is being applied when the user is not on the first page, but when the user is on the first page, the class is still being rendered. This suggests to me that I’m not using the isFirst() function correctly. Can someone help?

Could you try isLast() and see if it makes a difference? There’s a specific kind of issue (but not really) with page ordering in reverse, because of dates and this check is reversed

isLast() is also a function I would like to use but this doesn’t appear to be working either.

In case this makes a difference, I have set the page collection in the content file headers:


title: Key data
content:
items:
@page.children’: ‘/australia/general’

Folder structure is:

  • pages
    ** australia
    *** general
    **** 01.key-data
    ***** default.md

When a ResultSet object is initially created it is positioned before the first row, so .isFirst() will return false until you call .next() to position the ResultSet on the first row (if one exists).

1 Like