Skip to content

Kirby 5.6.1

$site

The $site object is the root element for any site with pages. It represents the main content folder with its site.txt.

How to access the $site object

The $site variable is automatically available in your templates and snippets.

To access the $site object in extensions, you have to define it first:

// via the kirby object
$site = $kirby->site();

// using the `site()` helper
$site = site();

Content fields

The $site class offers a magic caller for your content fields. Instead of getting them via $site->content()->get('your_field'), you can use the shorthand $site->your_field().

Reserved names

You cannot use the shorthand for fields with names that conflict with methods of the $site class or with custom site methods. Besides the methods listed above, this also includes internal methods that are not part of the documented API. The following names are reserved:

apiUrl, audio, blueprint, blueprints, breadcrumb, changeStorage, changeTitle, children, childrenAndDrafts, clone, code, content, contentFileData, createChild, createContent, createDefaultContent, createFile, createTranslations, decrement, documents, draft, drafts, errorPage, errorPageId, errors, exists, file, files, find, findPageOrDraft, getMethod, grandChildren, hardcopy, hasAudio, hasChildren, hasCode, hasDocuments, hasDrafts, hasFiles, hasImages, hasListedChildren, hasMethod, hasUnlistedChildren, hasVideos, homePage, homePageId, id, image, images, increment, index, inventory, is, isAccessible, isLocked, isValid, kirby, lock, mediaDir, mediaRoot, mediaUrl, modified, page, pages, panel, permissions, previewUrl, purge, query, readContent, root, save, search, site, storage, toArray, toSafeString, toString, translation, translations, update, url, urlForLanguage, uuid, version, versions, videos, visit, wasModifiedAfter, writeContent

To access a field with a reserved name, use $site->content()->get('fieldname'). We recommend prefixing your field names instead to avoid any conflicts.

Examples

Get the site title

echo $site->title();

Get any custom field of the site

echo $site->description();

Get the files stored in the content folder

$siteFiles = $site->files();

Get all the pages of the site

$pageIndex = $site->index();

Get the URL of the site

$url = $site->url();