$site->index() fetches all pages, including modules, don’t think that’s what you want. I don’t know your page structure and I don’t know what exactly you are trying to do, so it’s a bit difficult. Maybe you can provide some more information about your page structure and what you want to output ?
A custom collection or limiting the depth and visibility might be helpful.
Why are you filtering the collection again? If you want to filter by visible, it would be more performant to to filter before you filter by anything else.
Maybe you should do some dumps in the right places to see if the output is what you expect it to be in the first place.
I finally realised what makes those empty divs, it’s the parent page of a subpage. When there are subpages, I don’t want to output anything from its parent page, so the logic would go along the lines of:
→ if page has subpage, take only subpage, else take page
I think for this there needs to be some if/else code, but am thinking how magic it would be to filter it out already from the root $collection altogether.
Look’s like you are only using the $collection variable once, so you can loose that line and do this instead:
Is it essential to use a lower case page title as an ID? I normally use uid() for this, so you can shorten that a little. Will give you the page directory name.
It makes sense to store the collection in a variable, even if you don’t reuse it. You can put that bit into a controller if you want or at the top of the template, so you can change it more easily later if required (rather than looking for the collection further down in your template. Also there might be a reason for using the title instead of the UID.
Sure, it’s just @afincato was looking to reduce the code and improve performance, i figured that $c->uid is probably much faster for Kirby to figure out then $c->title()->lower()->html(). If all that is needed is some kind of hook for CSS or JavaScript, it works great.
Yes, I usually also use the UID, because Kirby doesn’t have to read the file, but title or UID, both may fall short if the user is allowed to change them.
My main point, however, was that I wouldn’t recommend dumping the variable prior to looping through the collection.
Using ->uid() is indeed much better, I forgot about it. I usually tell clients to update also the url link if they change the title after they created the page. But as you point out, it’s a possible problem if they don’t…