The next big object you should know about, is the $pages
variable. It contains every page of your site and you can browse it very fast and easy. The way you filter and search through it is highly inspired by jQuery, so if you are familiar with that you should get along very fast. If not, you are totally lost – just kidding :)
This is the most common way to search through all pages by uri to get a page object back. Take a look at the example:
<?php
$aboutus = $pages->find('about-us');
echo $aboutus->title();
// will echo "About us"
?>
<?php $project = $pages->find('projects/project-1') ?>
<a href="<?php echo $project->url() ?>"><?php echo $project->title() ?></a>
This is the same as the globally available $page
variable. It will return the current page object. You should use $page
instead.
Find the open page in a set. Read more about $page->isOpen here
This is a nice way to find a page by a specific key and value. Let's say the contact page has a field street:
content/contact/contact.txt
Street: Sesamestreet
----
City: New York
----
…
You could then get that page like this:
<?php $contact = $pages->findBy('street', 'Sesamestreet') ?>
Pretty cool :)
Find a page by UID. UID is the name of the folder without the prepended number. The UID of the "01-about-us" folder would be "about-us"
While findByUID takes the name of the folder without the number in front, this one searches for the entire dirname.
Well, that should be easy to get :)
Find the first page in a set of pages
Find the last page in a set of pages
Count the number of pages in a set
<?php echo $page->children()->count() ?>
or
<?php $pages->count() ?>
Returns a set of visible pages. For example if you want to find all visible children of the current page, do it like this:
<?php $visibleChildren = $page->children()->visible() ?>
If you want to get all visible first level pages, do this:
<?php $rootChildren = $pages->visible() ?>
Count all visible pages in a set of pages.
Returns a set of invisible pages.
Count all invisible pages in a set of pages.
Returns a set of pages without the page with the given UID
<?php
$set = $page->children->without('project-1');
?>
is a shortcut for $pages->without($uid) to have a more jquery way of doing this.
If you want to get a subset of pages, use the slice method to set the offset and limit.
<?php
$set = $page->children->slice(2);
// get all children starting with the third one.
$set = $page->children->slice(2,5);
// get five children starting with the third one.
$set = $page->children->slice(0,5);
// get five children starting with first one.
?>
Limit the result of pages in a set.
<?php
$set = $pages->limit(5);
// only get the first 5 pages (is equal to $pages->slice(0,5))
?>
Get a set of pages, starting with an offset.
<?php
$set = $pages->offset(5);
// starting with the sixth element of a set (is equal to $pages->slice(5))
?>
Returns a set of pages in reversed order. If you sort your folders with the appended numbers, it is sometimes easier to reverse the default sort so higher numbers will be returned first. For example if you build a blog and your article folders are named like this:
…you probably want to get a set of pages where 03-third-article is first. That's where $pages->flip()
is just doing what you want.
Sort a set of pages by any field and direction:
<?php
$sortedSet = $page->children->sortBy('dirname', 'desc');
$sortedSet = $page->children->sortBy('title', 'desc');
$sortedSet = $page->children->sortBy('date', 'asc');
?>
Returns the set of pages in random order
<?php
$random = $page->children->shuffle();
?>
Pagination sucks!! It's always a hassle to set that up right so Kirby implements a very nice way to paginate through a set of pages.
First you call the paginate($limit) method on a set of pages. This will add a pagination
object to the set. Set the limit to the number of pages you want to get per page.
<?php $list = $page->children()->paginate(10) ?>
Do something with that list of 10 pages
<ul>
<?php foreach($list as $item): ?>
<li><?php echo $item->title() ?></li>
<?php endforeach ?>
</ul>
Add previous
and next
links
<?php if($list->pagination()->hasPages()): ?>
<?php if($list->pagination()->hasNextPage()): ?>
<a class="next" href="<?php echo $list->pagination()->nextPageURL() ?>">next</a>
<?php endif ?>
<?php if($list->pagination()->hasPrevPage()): ?>
<a class="prev" href="<?php echo $list->pagination()->prevPageURL() ?>">previous</a>
<?php endif ?>
<?php endif ?>
Here's a list of all available methods of the pagination object:
Returns the current page
Get the total number of pages of entries.
Returns true if there's anything to paginate at all and false if not.
Return the total number of entries.
Generate an url to a specific page of a list.
Return the page number of the first page of a list.
Checks if the current page is the first page
Returns the url to the first page.
Returns the page number of the last page of a list.
Checks if the current page is the last page
Returns the url to the last page.
Returns the page number of the previous page of a list.
Checks if there's a previous page
Returns the url to the previous page.
Returns the page number of the next page of a list.
Checks if there's a next page
Returns the url to the next page.