Docs: The site, the visitor and the request
Site, visitor and request
The site, the visitor and the request: site, headExtras, visitor, panelLink, csrfField, formToken, hasModule, query, currentUrl, ownPath and the language calls.
The examples use the $esc shortcut from the overview.
The site #
site() #
The site's identity (P): {name, description, url, lang, logo, owner: {name, tagline}, social: {email, github, linkedin, website, x}, maintenance}. maintenance is new in 1.18.0.
<?php $site = $this->site(); ?>
<span class="brand-name"><?= $esc($site['name']) ?></span>
<?php if ($site['maintenance']): ?><div class="maintenance-bar" role="status">...</div><?php endif; ?>
site()is the only door to the settings table. It holds the name, address, logo and the owner's contact links, and nothing else. The table also keeps the SMTP host and the reCAPTCHA keys, which is why there is nosetting($key).- Every value is a string (an empty string when unset), except
maintenance, which is a bool for a staff banner. namefalls back to the product name.urlis the install's address with no trailing slash. From 1.18.0 it isSITE_URLfrom the install's config, and the setting only where that is unset.logois a same-site/uploads/path or an https address.langis the site's default content language.- Every
socialvalue is safe in anhref: an unsafe scheme is#and the email is amailto:. - It reads the settings once per request, shared with
seo().
headExtras() #
New in 1.18.0. {analytics_id, head_code}: the site's stored Analytics id (P; '' when none is set), and the custom head code an administrator entered under Settings ▸ Security. head_code is raw HTML, printed as it is, as the setting promises. It comes from the same single settings read as site(), so a theme never queries the settings table for either. Load the Analytics script through heldScript(), so cookie consent can hold it. See Sign-in, forms and consent.
<?php $extras = $this->headExtras(); ?>
<?php if ($extras['head_code'] !== '') { echo $extras['head_code']; } ?>
footerCopyright() #
New in 1.19.0. The footer's copyright line from Settings ▸ General (E), where {year} is the current year and {site} the site's name. It is an empty string when the site owner left it empty. Print it where your footer's base line goes, and fall back to your own line when it is empty. A theme that never calls it gets the line from core, as a paragraph before the last </footer>.
<?php $line = $this->footerCopyright(); ?>
<p class="base"><?= $line !== '' ? $line : $esc($this->site()['name']) ?></p>
hasModule($id) #
Whether a module is installed, enabled and running. Use it to show a band or a link only where the module runs. Switching a module clears the page cache, so a cached page agrees. To link into a module, use section() ("Actions and sections").
<?php if ($this->hasModule('blog')): ?><?php $blog = $this->section('blog'); ?><a href="<?= $blog['url'] ?>"><?= $blog['title'] ?></a><?php endif; ?>
The visitor #
visitor() #
Who is looking (P): {signed_in, name, verified, staff, initials, tint, color_mode, account_url, login_url, logout_url, appearance_url, profile_url, picture_url, notifications_url, notifications_on, notifications_unread, notifications_label, messages_on, messages_unread, messages_label, messages_url}. tint is an integer. initials to appearance_url are new in 1.18.0, and profile_url and picture_url in 1.19.0. The four notifications_ fields are new in 1.20.0 and the four messages_ fields in 1.21.0. There is no numeric id, email or role field. The member's id appears only inside profile_url (/members/<id>-<name>), where it is public in every profile link.
<?php $me = $this->visitor(); ?>
<?php if (!$me['signed_in']): ?>
<a href="<?= $esc($me['login_url']) ?>">Sign in</a>
<?php else: $panel = $this->panelLink(); ?>
<span class="acct-av acct-tint-<?= max(0, min(5, (int) $me['tint'])) ?>" aria-hidden="true"><?= $esc($me['initials']) ?></span>
<a href="<?= $esc($me['account_url']) ?>">Account</a>
<?php if ($panel !== null): ?><a href="<?= $esc($panel) ?>">Control panel</a><?php endif; ?>
<form method="post" action="<?= $esc($me['logout_url']) ?>"><?= $this->csrfField() ?><button type="submit">Sign out</button></form>
<?php endif; ?>
That is the default theme's account menu, shortened.
- A guest, or a session the account has since ended, is
signed_in,verifiedandstafffalse, with an emptyname. A signed-innameis the member's display name, else their own handle (shown to themselves only).verifiedis whether they confirmed their email address.staffis true when their role may enter the admin panel. profile_urlis the member's own public profile address (/members/<id>-<name>, built from the display name, never the handle). It is an empty string for a guest. Visitors still get a 404 there until the member has posted something they can see.picture_urlis the member's own profile picture, or an empty string when they have none or are a guest. Draw it with theperson-avatarcomponent.login_urlcarries the page it is drawn on as?return=, so a sign-in comes back there. Print it as given, never/account/loginwritten out.account_urlandlogin_urlhave no language prefix, because the account pages are not translated.- A signed-in answer takes the page out of the page cache, because the name is one person's. A guest answer is the same for every guest and stays cacheable.
initialsis one or two upper-case letters from the display name, for an avatar. It is'?'when the name has no letter, and an empty string for a guest.tintis an integer 0 to 5 that stays with the account through a rename. Use it to pick one of six avatar colours your theme defines. A guest is 0.color_modeis the member's own choice:auto,lightordark. A guest isauto. The owner's choice wins: honourcolor_modeonly while your theme's own colour option isauto, and offer the Auto, Light and Dark choice only then.appearance_urlsaves the choice. Send a POST ofmode(auto,lightordark),return(the root-relative page to come back to; anything else goes home) andcsrfField(). WithAccept: application/jsonit answers{"ok":true,"mode":"dark"}, or{"ok":false,"error":...}with a 4xx status, so a script can repaint at once. A plain form post redirects back.- The
messages_fields (since 1.21.0):messages_onis a boolean, false for a guest, under View as and while Messages is off.messages_unreadis the number of messages the reader has not read, an integer: never their own, nor those of anyone they block.messages_labelis a ready phrase such as "Messages, 3 unread", or an empty string while Messages is off.messages_urlis/account/messages. - Under View as,
visitor()answers for the person or role being viewed, and the page is never cached. logout_urlsigns out. It takes a POST withcsrfField()and is never a link, because any page could load a sign-out address.
panelLink() #
New in 1.18.0. The control panel's address (P, root-relative) for a signed-in visitor whose role may enter it, else null. The operator can move the panel off its default address, and the address is kept from anyone who is not staff. So never write the panel's address into a theme. Draw the link only when panelLink() gives one. Staff whose panel sign-in has lapsed still get it, and the panel asks them to sign in there.
<?php $panel = $this->panelLink(); ?>
<?php if ($panel !== null): ?><a href="<?= $esc($panel) ?>">Control panel</a><?php endif; ?>
csrfField() #
New in 1.18.0. The hidden token input for a POST to logout_url or appearance_url (E). It prints nothing for a guest, whose page may be cached.
<form method="post" action="<?= $esc($me['logout_url']) ?>"><?= $this->csrfField() ?><button type="submit">Sign out</button></form>
formToken() #
New in 1.18.0. The session's CSRF token for a form on a page that is never cached (P). It answers a guest too, so calling it takes the render out of the page cache. It is for the sign-in and registration forms of the account pages. Use csrfField() everywhere else.
<input type="hidden" name="csrf_token" value="<?= $esc($this->formToken()) ?>">
viewerSeesPresence() #
New in 1.21.0. True when this reader sees online status at all. It only decides whether your theme draws the hidden dot and loads presence.js; it shows nobody's status itself. The person-avatar component takes a presence_id and draws a .person-presence wrapper with a hidden __dot, which core's presence.js fills in from GET /members/online?ids=. Colour the dot with the token --cms-online (core falls back to #16a34a).
messageButton(int $userId, string $class = 'account-btn') #
New in 1.21.0. The button that starts a message to a member, as escaped HTML, or an empty string when Messages is not offered here. It never reveals that someone has blocked the reader. $userId 0 draws the New message button of the Messages page.
<?= $this->messageButton($memberId) ?>
The message-envelope component is the header's link to Messages with the unread count, shown as "9+" from ten. Scope its CSS to your header, for example .site-header .dm-envelope.
restrictionNotice(string $type, string $scopeType = '', int $placeId = 0) #
New in 1.21.0. The notice a restricted member reads where they would have written, as {type, label, sentence, date, date_iso, place, reason, appeal, appeal_url}, all escaped, or null when this reader is not restricted in that way (also for a guest, under View as and for an unknown type). The restriction-notice component draws it.
The request #
query($key, $kind = 'text', $default = null) #
One value from the address's query string, in the shape you ask for, else $default (E for text; int, slug and a choice are safe as they are).
'int': a whole number from 1 to 1,000,000.'text': trimmed, control characters removed, at most 200 characters, escaped.'slug':a-z,0-9and-, at most 100 characters.- A list of allowed strings:
query('sort', ['new', 'old'], 'new').
A repeated key (?q[]=x), an empty value or an unknown kind gives $default. The key url is reserved, so query('url') is always $default. Reading a key the address carries keeps the page out of the page cache.
<?php $q = $this->query('q'); $n = $this->query('page', 'int', 1); ?>
currentUrl($set = []) #
This page's own address in the request's language, with the query values you set (E). It is the page's canonical path, never the address as the visitor typed it. A route that declared none gives its first segment (for example the Blog's address). A value that is not a string or an integer is left out.
<a href="<?= $this->currentUrl(['page' => $n + 1]) ?>">Next page</a>
ownPath() #
New in 1.18.0. currentUrl()'s path without a language prefix or query (P): the page's canonical path, else its route's first segment, else /. It never hands back what a visitor appended to a route (a route answers under any sub-path they make up), which makes it the safe return value for a form that comes back to this page.
<input type="hidden" name="return" value="<?= $esc($this->ownPath()) ?>">
lang() and contentLang() #
The language codes of the request and of the page's content. Calling either claims <html lang>.
<html lang="<?= $this->contentLang() ?>">
languages() #
The language switcher as {code, name, url, current, available} (P), or [] while no extra language is on. Calling it claims the switcher.
<?php foreach ($this->languages() as $l): ?><a href="<?= $esc($l['url']) ?>"><?= $esc($l['name']) ?></a><?php endforeach; ?>
langUrl($path) #
A same-site path in the request's language (P). A full URL or //host comes back unchanged.
<a href="<?= $esc($this->langUrl('/contact')) ?>">Contact</a>
New in 1.20.0 #
isHome(): true when the page shown is the site's home, whichever page or module the owner chose. Use it instead of comparing a path with "home".notifications(): the signed-in member's own notifications, newest first, with a cursor for more.visitor()gains four notifications fields.
New in 1.21.0 #
visitor()gainsmessages_on,messages_unread,messages_labelandmessages_url.viewerSeesPresence(): whether this reader sees online status.messageButton(): the button that starts a message to a member.restrictionNotice(): a restriction's notice for the member it applies to.