Docs: Fields and pictures
Fields and pictures
The page being shown: field, fieldRaw, hasField, boundFields, rows, related, pictures, picture, image and link.
These calls read the page being shown. The examples use the $esc shortcut from the overview.
The page and its fields #
field($key, $default = '') #
A field's value as text (E; M for a rich-text or HTML field). You get your default if the Layout does not declare the field, a condition hides it, it is empty, or it is a list.
<?= $this->field('intro') ?>
fieldRaw($key, $default = '') #
The same value, unescaped (P). It exists for a helper that escapes into an attribute itself, where field()'s escaped string would arrive double-encoded and break a URL that carries an &. Echoing it into markup is a bug for every type except rich text and HTML.
<meta name="x" content="<?= htmlspecialchars($this->fieldRaw('intro'), ENT_QUOTES, 'UTF-8') ?>">
hasField($key) #
Whether the Layout declares the field, it shows, and this page stored a non-empty value in it.
<?php if ($this->hasField('intro')): ?><p><?= $this->field('intro') ?></p><?php endif; ?>
boundFields() #
The fields this page shows, as [key => type], in the Layout's order.
<?php foreach ($this->boundFields() as $key => $type): ?>
<?php if (!$this->hasField($key) || in_array($type, $this::FIELD_NO_VALUE_TYPES, true)) { continue; } ?>
<?php if ($type === 'gallery'): ?>
<?php foreach ($this->pictures($key, ['class' => 'page-gallery-img']) as $picture): ?><?= $picture ?><?php endforeach; ?>
<?php elseif (in_array($type, $this::FIELD_LIST_TYPES, true)): ?>
<ul><?php foreach ($this->rows($key) as $row): ?><li><?= is_array($row) ? implode(' — ', $row) : $row ?></li><?php endforeach; ?></ul>
<?php elseif ($type === 'relation'): ?>
<ul><?php foreach ($this->related($key) as $rel): ?><li><a href="<?= $rel['url'] ?>"><?= $rel['title'] ?></a></li><?php endforeach; ?></ul>
<?php else: ?>
<?= $this->field($key) ?>
<?php endif; ?>
<?php endforeach; ?>
That is the default theme's generic page view, shortened. It renders whatever Layout an install defined. A theme that knows its own Layouts names the fields directly. Note what is not there: no json_decode, no htmlspecialchars, and no check of the type before echoing. The API escapes per declared type, including types that did not exist when your theme was written. Test for gallery before the list branch, because a gallery is one of $this::FIELD_LIST_TYPES.
rows($key) #
A taglist or multiselect as a list of strings, a repeater as rows, a gallery as {image, alt} rows (E). Any other type gives [].
<?php foreach ($this->rows('faq') as $r): ?><dt><?= $r['q'] ?></dt><dd><?= $r['a'] ?></dd><?php endforeach; ?>
related($key) #
A relation field's published targets as {id, title, url}, in the order the editor arranged them (E). A target that no longer exists is dropped. A post shows once it is due.
<?php foreach ($this->related('see_also') as $r): ?><a href="<?= $r['url'] ?>"><?= $r['title'] ?></a><?php endforeach; ?>
A field its condition hides #
A Layout author can say "only show this field when...". On a page where the condition fails, the API answers as if the Layout did not declare the field. field() gives your default, hasField() is false, rows(), pictures() and related() give [], boundFields() leaves it out, and a hidden sub-field is missing from its row. Code that already checks hasField() needs no change. $page['page_data'] still holds the hidden value, which is one more reason never to decode it.
The page body is $page['page_contents']. It is markup sanitised when saved, so echo it as it is.
Images and links #
picture($source, $alt = '', $attrs = []) #
One image as <picture> markup with width and height (M). $source is an image field's key, or an item from pages(), page(), posts() or a gallery's rows(). The image is lazy-loaded. New in 1.18.0: pass 'loading' => 'eager' in $attrs for an image in the first screen, such as a post's cover.
<?php $cover = $this->picture($p, '', ['class' => 'post-card-img', 'sizes' => '(max-width: 768px) 100vw, 352px']); ?>
<?php if ($cover !== ''): ?><div class="post-card-cover"><?= $cover ?></div><?php endif; ?>
The address is judged. An item from a listing arrives escaped, so picture($item) decodes it exactly once and accepts a file in the media library, a same-site /uploads/ path or an https address. Anything else, such as a javascript: or //host/ address, gives an empty string. An empty $alt is taken from the item's alt or title. $attrs are extra attributes for the image element, escaped for you.
pictures($key, $attrs = []) #
A gallery as a list of ready-made <picture> strings (M). You get [] when $key is not a gallery or is hidden.
<?php foreach ($this->pictures('gallery', ['class' => 'tile']) as $p) { echo $p; } ?>
image($source) #
{url, width, height, webp, srcset, sizes} for markup of your own, such as a CSS background or a <source> (E). It is null when picture() would give an empty string. srcset and sizes are new in 1.18.0.
<?php $logo = $site['logo'] !== '' ? $this->image(['image' => $esc($site['logo'])]) : null; ?>
<?php if ($logo !== null): ?><img alt=""<?= $logo['width'] !== null ? ' width="' . $logo['width'] . '" height="' . $logo['height'] . '"' : '' ?> src="<?= $logo['url'] ?>"><?php endif; ?>
Narrower sizes #
The media library keeps WebP copies of each image at 320, 640, 960, 1280 and 1600 px. Only sizes narrower than the image are made, and a GIF gets none. picture() and pictures() list the copies made so far in the WebP <source>'s srcset, so a phone downloads a small file. So do the images in a rich-text field and a post's body.
Since 1.20.0 picture() and image() also list the original, last and at its own width, when the WebP copy is narrower than the original because Media's largest size capped the copies. A wide or high-density screen then gets a sharp image. An image in a rich-text field or a post's body gets the same when its width attribute is wider than the copy. pictures() does it only when you pass a width in $attrs.
The default sizes assumes the image is shown no wider than its width. Pass ['sizes' => '(max-width: 600px) 100vw, 50vw'] in $attrs when your layout shows it narrower. image() gives the same srcset and sizes. webp is the WebP copy's address. webp and srcset are null when the library has no WebP copy. sizes is null, and srcset the WebP copy alone, while no narrower size exists and the original is not listed.
There is no resizing call. A visitor's request never writes a file, and the width and height come from the file as it is (null in image() for an image on another site).
An image in a rich-text field or a post's body comes wrapped in <picture> once it has a WebP copy. Style it with a descendant selector (.prose img), not a child one (.prose > img).
link($key) #
A URL field as an href (E). A scheme that could run script gives #. An empty value or any other field type gives an empty string. field() gives a URL field's value as typed, so a link built on it is not judged. link() is.
<a href="<?= $this->link('cta_url') ?>">Get started</a>
Since 1.19.0 the site owner can turn on Settings ▸ General ▸ "Open links to other sites in a new tab". Then links to other sites in a rich-text or HTML field, in rows() and in a post body come with a new-tab target. Core does it, so your theme does nothing.