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'sminlengthand its hint. New in 1.18.0.FIELD_LIST_TYPES: the field types that hold a list. Print them withrows(), orpictures()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 oneposts()call hands out. A longer list is read in slices withoffset. 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, andheadExtras()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 throughvisitor(),query()andcurrentUrl().- 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()andmay().
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.