How to pull information sub folders


#1

Hi,

I am still learning Kirby. I trying to figure out how to pull information from separate sub folders. Can someone please help.

Tours ( Main)
* USA
* CANADA
* RELIGIOUS
* etc…

Thank you


#2

It really depends what you want to do with that data, and there are several ways to do it, but you could go with this in a template…

<?php
$subpages = $site->find('tours')->children() // get all the subpages under the tours page
?>

<?php foreach($subpages as $subpage): ?>
  <h2><?= $subpage->title()->html() ?></h2>
  <!-- more code here... -->
<?php endforeach ?>

That will list out the title of all subpages under the tours page.

What exactly are you trying to do? We can help a little more if you describe the desired end result. Is it a list you want or information from a very specific subpage only?


#3

That piece of code is not really recommendable. Always check if an object exists before calling a method:

if($page = page('tours')):
   $subpages = $page->children();
  foreach($subpages as $subpage):
    // do something
  endforeach;
endif;

#4

Thank you for the information but can I use isChild?

<?php if($page->isChildOf('tours')): ?>

// do something

<?php endif ?>

#5

Well, yes, you can, but the question is what exactly you want to achieve. As @jimbobrjames already said, we need some more context to really help you.

If you are on the parent page and want to pull information from subpages, then isChildOf() won’t help you.


#6

@texnixe

I am trying to show on a parent page a list of different tours (different category tours). Then I want the sub-page to show all tours from one of the category. Once a single tour is selected it should show a detail page. Below is how I have it map out on paper but it is not coming out in Kirby. Hopefully this helps.

Parenet page category list
Canada (Parent) show all
------Tours (Subpage) Select
----------Tour Detail (Detail Page) Read

Thank you


#7

So if I get that right, your setup is like this:

parent page: TOURS

  • category subpages: USA, CANADA etc.
    • in each category, subpages with tours for each category

In your tours template, loop through the categories:

tours.php

<?php
foreach($page->children()->visible() as $category): ?>
<a href="<?= $category->url() ?>"><?= $category->title() ?></a>
<?php endforeach ?>

The do the same in the category template:

category.php

<?php
foreach($page->children()->visible() as $tour): ?>
<a href="<?= $tour->url() ?>"><?= $tour->title() ?></a>
<?php endforeach ?>

And finally in the individual tour page

singletour.php

<?= $page->title() ?>
<?= $page->text()->kirbytext() ?>

The above assumes that the text files in those different page levels have the corresponding filenames. Otherwise, you have to adapt this.


#8

@texnixe

Yes, would it be best for me to create a category in the blueprint ?

I thank everyone for their help…I wish I can buy you all a beer.


#9

Instead of using subpages for the categories, you can also have a flat structure with all the tours as subfolders in the tours parent and use a field in your singletour blueprint for the category, yes, that is possible.

Then instead of fetching the category folders in your tours.php, you would fetch the categories. Then to show the pages that belong to a given category, you would have to use parameters in your URL or use routes to create those virtual category pages.


#10

What would you recommend category or flat structure ? I am learning and new to kirby


#11

It is definitely easier to set this up with dedicated folders for the categories.

But you would be more flexible to change categories and moving pages between categories with a field you can filter by.

Another argument for using subfolders could be the amount of pages we are talking about, because many subfolders can slow down Panel and frontend performance if Kirby has to read the files. But that is only relevant once we start talking about more that a couple of hundred of subpages and not if there are only 50 or so.


#12

It would be more then 50 pages for each categories. Do you know if Kirby have information on building categories?


#13

How many categories are there in total and roughly how many pages in total, across all the categories?

Just kind of thinking out loud…Looking at your very first post, you have a country category and a type category (Religious). It might make sense to use two Tag Fields so you can tag with “Religious” and “USA”. That way you can filter on all USA tours and then again by a more granular type of “Religious”. It will also allow you to list all Religious tours regardless of country.

And then organise them in the folder structure by type rather then country, since i am guessing your list of tour types is much shorter then the countries the tours are in?

So you end up with this inside your Tours top level folder:

Religous tours (Folder)
-- USA (Subfolder)
---- Tour 1 Details (Page)
---- Tour 2 Details (Page)
---- Tour 3 Details (Page)
-- Canada (Subfolder)
---- Tour 1 Details (Page)
---- Tour 2 Details (Page)
---- Tour 3 Details (Page)
Sightseeing Tours (Folder)
-- USA (Subfolder)
---- Tour 1 Details (Page)
---- Tour 2 Details (Page)
---- Tour 3 Details (Page)
-- Canada (Folder)
Cruises (Folder)
-- USA (Subfolder)
---- Tour 1 Details (Page)
---- Tour 2 Details (Page)
---- Tour 3 Details (Page)
-- Canada (Subfolder)
---- Tour 1 Details (Page)
---- Tour 2 Details (Page)
---- Tour 3 Details (Page)

My thinking is that it would break the articles up into smaller chunks, which would be faster to filter through.


#14

Not quite sure what you mean, but apart from using subpages as categories, there are some field types that lend itself to be used for categorising your pages:

  • tag field: apply any number of categories with autocompletion
  • select field: to select a single option from a given list of categories
  • checkboxes: if the list of options is not too long and the user should be able to select multiple options
  • radio field: if the list of options is not too long and the user should be limited to selecting exactly one option

Select, checkboxes and radio fields options can be made more dynamic if they query another field where possible options can be defined.

The values from these fields can then be used to group your page collections.

You can also combine the above fields for more fine grained taxonomies.

Further to @jimbobrjames suggestion, another option would be to use folders for either countries (or types), and then use a field to further categorise within these top categories. That would at least avoid a deeply nested structure.

I think the first decision you should make is what is the best sort of structure for your content. Which categories are fixed, which are rather dynamic and take it from there.

Folders are useful if one item belongs to exactly one category, so I would use folders for the categories that apply one once. Fields are the better option if the item belongs to more than one category.