Displaying subfolder pages in a list

Hello,

I am building a one pager website using kirby and I am facing a newbie-kind issue. I searched in the archive but didn’t find my answer. I’ll try to explain it to you the clearer i can.

First, here is the folder architecture I built:

Summary

Root
–Folder 1
----Subfolder A
-------Page a
-------Page b
-------Page c
-------Page …
----Subfolder B
-------Page a
-------Page b
-------Page c
-------Page …
----Subfolder C
-------Page a
-------Page b
-------Page c
-------Page …
–Folder 2
–Folder 3

I displayed folders 2,3,… and their content without any problem but I would like to display in the same homepage three lists corresponding to the content of subfolder A,B and C. I don’t have any problem to display the title of the subfolders but can’t figure out how displaying the name of the pages.

Here is my code:

<?php 
$subfolderA = page('subfolderA')->children()->visible();
 if(isset($limit)) $subfolderA = $subfolderA->limit($limit)
;?>

<ul class="list">

  <?php foreach($subfolderA as $subfolderA): ?>

	<h4><?= $subfolderA->title()->html() ?></h4>

    	<li class="liststyle"></li>

  <?php endforeach ?>

</ul>

I hope you understand my issue and can help me. Anyway, thanks for this amazing CMS.

Simon.

I think your problem is here, you have a duplication. Try:

  <?php foreach($subfolderA as $subfolderItem): ?>
	<h4><?= $subfolderItem->title()->html() ?></h4>
    	<li class="liststyle"></li>
  <?php endforeach ?>

Also, your h4 element should be a child element of a UL, rather it should be inside the LI.

What does programme refer to?

While I don’t think this is causing a problem, it is definitely not good coding style. Ideally, you use something that connotates a list (a plural noun or a collection) for a collection of items, and a singular for the variable that is used inside the loop (unless the inner variable stands for another collection or array). Makes your code a lot easier to understand.

If you want to fetch all children of subfolderA, you would have to it like this:

$subfolderA = page('folder1/subfolderA')->children()->visible();

The page helper needs the complete path to the page.

Or, alternatively:

$subfolderA = page('folder1')->children()->find('subfolderA')->children()->visible();

Please note that this code will fail with an error if the page doesn’t exist. To be on the safe side, we should be a bit more verbose:

if($p = page('folder1/subfolderA')) {
  $subfolderA = $p->children()->visible();
}

The reason is that children() is a method of the page class, so we need a page object, otherwise we get an error. This is a typical problem if you call a page something during development and later rename or delete it. The same is true for all other objects like files etc.

Thanks you both for answered my question. Unfortunately I didn’t really understood your answers, and it’s all my fault because I didn’t explain AND wrote well my code. Since, I solved my problem with a kind of “folder architecture trick” which is really unlogical regarding my website content but it’s working… I apologize for your time and think you can delete this thread. Simon.

@Simon1:
Did you ever looked at Kirby-Docs: One-pager?

I think you find there code, what you can use…

Yes I did thank you. I was not far away from a valid solution by using the code “grandchildren” but didn’t find the way to group the items into the right section. I just moved my subfolders to the root and displayed them with the main loop used in the Kirby-Docs: One-pager to fetch the sections. Hope you understand what i’m saying, I don’t usually discuss code in english language. Thank you.