Skip to content

Kirby 4.4.1

$page

The $page object is the heart and soul of Kirby. It is used to construct pages and all their dependencies like children, files, content, etc.

You can extend this set of methods with custom page methods or in a page model.

How to fetch the $page object

The $page class is available in Kirby's templates/snippets etc. through the $page variable that - unless otherwise defined – always refers to the current page. However, you can also define a $page object by calling a specific page using the page() helper:

$page = page('somepage');

Or getting it from a collection of pages

$page = $pages->first();

When getting a specific page via the page() helper or when trying to create a page object from field values stored in content, always verify that you have a page object before you call any of the Page methods.

Content fields

The $page class offers a magic caller for your content fields. Instead of getting them via e.g.

$page->content()->get('your_field')

you can use the shorthand

$page->your_field()

You can not use the shorthand for fields with names that conflict with the default methods of the $page class (see above) or used by custom page methods.

Examples

With the $page object defined, you can start using the methods of the class:

Get a field of the page

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

Get the children of the page

<?php $children = $page->children() ?>

Get all images of the page

<?php $images = $page->images() ?>

Check if the page is active

<?php e($page->isActive(), 'active') ?>

Check if the page has draft children

if ($page->hasDrafts()) {
  echo "This page still has some children that haven't been published yet";
}