Docs: Template API

The Template API

Every call a Tilspire theme can make, in groups. Start here: how each call returns its text, and what it does when nothing is found.

Everything a theme's view may ask of Tilspire is a method on $this. You read content, the site and the request through these calls, never through SQL, page_data, $_SESSION or $_GET. A query of your own forgets drafts and translations, and bypasses the page cache's rules. This section is current for Tilspire 1.21.0.

How text comes back #

Every call is marked with one letter. It is the only thing you have to remember about a call.

  • E, escaped. Echo it as it is. Never pass it through htmlspecialchars() again.
  • P, plain. Escape it as you print it: htmlspecialchars($v, ENT_QUOTES, 'UTF-8').
  • M, markup. Core cleaned it when it was saved, or built it. Echo it as it is.

The examples in these pages use the shipped default theme's escaper:

$esc = static fn ($v): string => htmlspecialchars((string) $v, ENT_QUOTES, 'UTF-8');

Two promises #

Nothing throws on a public page, with one exception: captchaBox() throws InvalidArgumentException for a form name it does not know. A list that cannot be built is [], or an envelope with items empty and error naming why. A lookup that finds nothing is null or an empty string.

A shape may gain keys in 1.x. A key is never removed or given another type.

Themes carry no rules #

A theme decides how things look. It never decides who may do what, or what state a thing is in. Everything you are given arrives decided: lists already filtered for the visitor, ready states, finished addresses and names, and a control as one call.

The server still checks every action when it is used. A control you print by hand gains you nothing, and a control you leave out is only a missing button. Do not test isStaff(), a role or a permission to show or hide something. Ask may(), or print action().

With maintenance mode on, core answers a visitor whose role cannot enter the panel with its own page (503, Retry-After, noindex) before your templates run. You write no gate. Staff get the site, so draw a banner when site()['maintenance'] is true.

Where each call is #

  • Fields and pictures: field, fieldRaw, hasField, boundFields, rows, related, picture, pictures, image, link.
  • Other pages and menus: pages, page, children, siblings, menu, breadcrumbs, pageSettings, pageTrail, pageChildren, pageUpdated, pageShare, pagePolicy.
  • Posts: posts, post, postCategories, postCategory.
  • Actions and sections: action, actions, may, section, searchEnabled.
  • The site, the visitor and the request: site, isHome, headExtras, visitor, notifications, viewerSeesPresence, messageButton, restrictionNotice, panelLink, csrfField, formToken, hasModule, query, currentUrl, ownPath, lang, contentLang, languages, langUrl, footerCopyright.
  • Sign-in, forms and consent: signupChecks, captchaBox, signinIdentifier, consent, consentBanner, consentLink, heldScript, resetFactor, rules, rulesLink, rulesAccept.
  • Head, assets and views: date, dateLocale, emojiWords, seo, icons, design, providesContainer, asset, assetVersion, assets, pushAsset, option, render, component, number, timeTag, siteIcon, pageHeadDefault, slot.
  • Search, comments and previews: searchResults, askBox, comments, commentsBlock, isPreview, previewed.
  • Tracker: trackers, trackerItems, trackerItem, trackerRoadmap, trackerSimilar, trackerForm.
  • Upgrading from 1.20, 1.19, 1.18, 1.17 and 1.16: the habits worth replacing.

Constants #

Core's limits and kinds, read as $this::NAME, so no theme has to name a core class.

  • PASSWORD_MIN_LENGTH: the shortest password core accepts, for a field's minlength and its hint. New in 1.18.0.
  • FIELD_LIST_TYPES: the field types that hold a list. Print them with rows(), or pictures() for a gallery. New in 1.18.0.
  • FIELD_NO_VALUE_TYPES: the field types that are editor furniture and publish nothing, such as a message. New in 1.18.0.
  • POST_MAX_LIMIT: the most posts one posts() call hands out. A longer list is read in slices with offset. New in 1.19.0.

What there is no call for, on purpose #

  • Writes of any kind. A view renders. A form posts to its own endpoint, with its token.
  • Raw SQL or a query builder. Every listing's rule (published only, translated, paged) lives in one place.
  • A generic setting($key). It would reach the SMTP host and the reCAPTCHA keys. site() is the only door to the settings table, and headExtras() comes from the same read.
  • Another member's data. There is no user($id) and no member list.
  • Resizing an image on a visitor's request, and relative dates such as "3 days ago" written by the server. timeTag() leaves that to the browser.
  • Content transforms, such as a table of contents. Anchors on headings are core's, when the site owner turns them on in Pages ▸ Settings.
  • $_SESSION, $_COOKIE, $_SERVER. Reach them through visitor(), query() and currentUrl().
  • A maintenance gate, and any rule about who may do what. Core settles maintenance first. Whether a control shows is the module's decision, handed to you as action(), actions() and may().

Calls that are not a theme promise #

Some methods are public only because index.php, a module's route or core's own code calls them, such as setPage(), setPost(), setSeo(), the inject*() family and formChecked(). They may change or go in any release, and a theme that calls one is unsupported. setCacheable() is the one meant for a module's route: call $template->setCacheable(false) when the page differs per visitor.

The longer guide to writing a theme is Themes.

2 Fields and pictures The page being shown: field, fieldRaw, hasField, boundFields, rows, related, pictures, picture, image and link. 3 Other pages and menus Reaching beyond the current page: pages, page, children, siblings, menu and breadcrumbs, with every listing option and error code. 4 Posts The Blog's posts in a theme: posts, post, postCategories and postCategory, and how to link to the Blog itself. 5 The site, the visitor and the request The site, the visitor and the request: site, headExtras, visitor, panelLink, csrfField, formToken, hasModule, query, currentUrl, ownPath and the language calls. 6 Head, assets and views Dates, the head and the files around a view: date, seo, icons, design, assets, asset, assetVersion, pushAsset, option, render and component. 7 Search, comments and previews Site search, comments and drafts: searchResults, askBox, comments, commentsBlock, isPreview and previewed. 8 Actions and sections Template API calls where core decides and the theme draws — action(), actions(), may(), section() and searchEnabled(), so a theme never hard-codes an address or a rule. 9 Sign-in, forms and consent Template API calls for forms a theme draws itself — sign-up checks, captcha boxes, CSRF tokens, the cookie banner and link, held scripts and the head extras an administrator sets. 10 Tracker The Template API calls for Feedback's Tracker — trackers(), trackerItems(), trackerItem(), trackerRoadmap(), trackerSimilar() and trackerForm() — with their shapes, errors and views. 11 Upgrading from 1.16 Five habits from 1.16 themes worth replacing, and the call to use instead of each. 12 Upgrading from 1.17 A checklist for a theme forked before Tilspire 1.18.0 — five habits that now cost something, four worth replacing, and the Template API call to use for each. 13 Upgrading from 1.18 A checklist for a theme forked on Tilspire 1.18.x, up to 1.19.2 — five habits that now cost something, five worth replacing, and the Template API call to use for each. 14 Upgrading from 1.19 A checklist for a theme forked on Tilspire 1.19.x, up to 1.20.0: three habits that now cost something, the parts that stay hidden until you place them, and the Template API call to use for each. 15 Upgrading from 1.20 What a theme forked before Tilspire 1.21.0 should change, what stays hidden until you place it, and what is new.