Template format and loading
On this page
Why this shape
- Today, to give a built-in screen a shape CSS can’t reach, you have to write it from scratch in TypeScript (
WebPlugin.screens.*). The code of the built-in screens (packages/web/src/system/*.ts) can’t be copied by a game. - Ren’Py starts from
screens.rpyinside the game, and TyranoScript from the class names insave.html, so you can begin by copying a working built-in screen and editing it. - If the built-in screens use the same format as games, a copy behaves the same as the built-in screen, and when an engine update changes the built-in screen, the difference from the original version can be shown.
The shape of a template file
<!-- Copied from the built-in confirmation screen (Liminalia 0.4.0, version 2) -->
<style>
.lm-dialog { border-radius: 0; }
</style>
<template data-part="screen" data-version="2">
<div class="lm-dialog-backdrop">
<div class="lm-dialog" role="alertdialog">
<h2 class="lm-sr-only" data-text="confirm.heading"></h2>
<p class="lm-dialog-message" data-field="message"></p>
<div class="lm-dialog-buttons">
<button class="lm-button" data-role="cancel" data-field="no"></button>
<button class="lm-button lm-primary" data-role="ok" data-field="yes" data-autofocus></button>
</div>
</div>
</div>
</template>
- One screen is one HTML fragment (no
<html>or<body>). - It is a list of
<template data-part="part name" data-version="version">. The whole screen is the partscreen. Things that repeat, like one save row, and things a game may want to replace on their own, are separate parts. - A part holds one element. If it holds two or more, the engine wraps them in
div.lm-template. - Text that is only indentation line breaks (whitespace between elements) is dropped on reading, so inline buttons don’t get gaps between them (it is kept inside
preandtextarea). - Elements outside
<template>are not used (they are dropped), except<style>. Comments can go anywhere.
Where CSS goes
- The look of the built-in screens stays in
default.css. Templates only carry class names. Reason: themes,ui.*values and the game’s CSS keep working on the same class names and CSS variables. - A template a game copied can have CSS in a
<style>in the same file. It goes into the document’s<head>the first time the screen is shown, and is put in again when the template changes. One file copies one screen, and one file shows its difference. - CSS for the whole game goes into the separate project CSS (
ui.cssinliminalia.json). A template’s<style>is only for that screen. - Another option: putting
confirm.cssin the same folder. That makes two files to read and register, and splits the difference of a copy into two, so it is not taken for now. A<style>applies to the whole document, so write selectors starting from the screen’s class name if it should not affect other screens.
Attribute vocabulary
Attributes creators write are data-* (without lm), and states the engine writes are data-lm-*. Creators don’t write data-lm-* (the engine overwrites them).
What pressing does (data-role)
Can go on any element. For an element that isn’t a <button>, the engine adds role="button" and tabindex="0" and makes Enter and Space press it.
| Value | Does | Sound |
|---|---|---|
ok | Decide (the confirmation’s “Yes”) | select |
cancel | Cancel (the confirmation’s “No”) | cancel |
close | Close the screen. Same as Esc, right click and gamepad B | cancel |
back | Go back one step (a step inside the screen; same as close if there is none) | cancel |
prev-page, next-page, page | Change pages and tabs | page |
System action names (save, load, backlog, config, title, exit and so on; SYSTEM_ACTIONS) | Do that action (menu, title, quick menu) | select |
Actions on a list row (pick, delete, protect, jump and so on) | Act on that row. Decided per screen | select |
- Each screen decides which values work, listed in a table in the screen document (screens.md). An unknown value looks pressable and does nothing. The dev server prints it to the console, and
lim checkand the Studio check report it (template-unknown-value). - Confirmation:
ok(yes),cancelandclose(no). Other screens are in “Parts and attributes of four screens”, “Parts and attributes of three screens” and “Parts and attributes of the settings screen”. - The sound on press can be changed with
data-sound="select|cancel|page|choice|none"(noneplays nothing, and also no hover sound). The built-in menu’s “Return to game” isclosebut plays select as before, and the ending name’s proceed button plays nothing as before. - Actions the screen can’t do right now (quitting in the browser version and so on) remove the element entirely (the engine passes the action as null). Actions that can’t be pressed (a locked choice) get
data-lm-disabledandaria-disabled, anddisabledon a<button>.
Putting in text (data-field, data-text)
data-field="name": puts in the text of a screen value. The engine replaces the contents. Each screen decides the names. The confirmation hasmessage,yes,noandkind.data-text="key": puts in built-in wording (a key ofTEXTinstrings.ts, such asconfirm.no). It becomes the wording a game replaced withWebPlugin.texts, in the language the player chose.- Without either, the written text is shown as is (for words only the game uses).
data-attr-<attribute>="name": writes a screen value into that attribute (data-attr-aria-label="spoken",data-attr-src="image",data-attr-aria-pressed="auto",data-attr-data-kind="kind"and so on). If the value is missing or empty, the attribute is removed. Attributes starting withon,srcdoc,target,actionandformactionare not written. Attributes that take a URL never getjavascript:and the like.data-if="name": if that value (a screen value, or a per-screen show flag) is empty (missing,"",0, false), the element is removed entirely, and attributes inside it don’t work. Used for the tabs of the controls help (not shown without pages), the time bar of choices, and the kind text of the ending name.
Lists (data-list, data-item)
<ol class="lm-slot-list" data-list="slots" data-item="slot"></ol>
<template data-part="slot" data-version="1"><li class="lm-slot">…</li></template>
- The contents of a
data-list="list name"element are replaced with onedata-itempart per item. A row is a part, so a game can replace just the row. data-fieldanddata-roleinside a row are the row’s values and actions (not the outer screen’s). Lists can nest.- Writing
<template data-list="name" data-item="part">places rows right there without a wrapping element (rows become children of the outer element). Used where older CSS assumes children, like.choices > buttonof choices. A marker comment stays where it was placed (used when rebuilding; it doesn’t affect CSS). - One list can mix different parts (the engine picks a part per row: image buttons of choices, the table and pages of the controls help).
data-itemis the default part.
Tying to settings (data-setting)
Works only on the settings screen (added in stage f3; “Parts and attributes of the settings screen” below).
<div class="lm-config-row" data-setting="bgm">
<span class="lm-config-label" data-text="config.bgm"></span>
<input class="lm-range">
<output data-field="bgm"></output>
</div>
<div class="lm-config-row" data-setting="skipUnread">
<button class="lm-toggle" data-value="false" data-text="config.skipRead"></button>
<button class="lm-toggle" data-value="true" data-text="config.skipAll"></button>
</div>
data-setting="setting name": goes on an item’s row. The name is a player setting (Settingsinsettings.ts). A game’s own setting iscustom.name. Removing a row removes the item, and reordering rows reorders items.- Controls inside the row: an
<input>is a slider (the engine makes ittype="range"and takes the range and step from the setting), an element withdata-value="value"is one choice (pressing sets that value, and the pressed one getsaria-pressed="true"), and an element withdata-toggleis a switch (on and off;role="switch",aria-checked,data-lm-on). One row may have both a slider and choices. - The text of
data-valueis read in the shape of the setting’s value (trueandfalsefor a true/false setting, a number for a number setting). - For a slider of a game’s own setting, write
min,maxandstepon the<input>(without them, 0 to 100 in steps of 1). - Items that can’t be used right now (the screen mode where fullscreen isn’t available, desktop-only items, items the game doesn’t use) are removed with their row by the engine even if written.
- An unknown name does nothing (the dev server prints it to the console, and
lim checkand the Studio check reporttemplate-unknown-value).
Another option: putting data-setting on the control itself (the <input> or the row of choices). The unit of showing, hiding and reordering is the row, so without it on the row, only the name of an unusable item would remain. On the row, one attribute reads as “this row is this setting”.
States (data-lm-*)
The engine writes these on elements, and CSS targets them. Valueless markers are present only while in that state.
| Attribute | Meaning | On |
|---|---|---|
data-lm-hover, data-lm-focus, data-lm-pressed | The mouse is over it, it’s selected by keys, it’s being pressed | data-role elements (same as today’s UI parts) |
data-lm-disabled | Can’t be pressed (with aria-disabled) | data-role elements, list rows |
data-lm-selected | Selected by the first touch of a finger | data-role elements |
data-lm-read | Already read (backlog, choices, extras) | List rows |
data-lm-protected | A protected save | List rows |
data-lm-empty | An empty slot | List rows |
data-lm-current="page" and so on | The current page, tab or value | List rows, tabs |
data-lm-new | Newly unlocked | List rows |
data-lm-locked | Not unlocked yet (extras) | List rows |
data-lm-on | A switch turned on (data-toggle on the settings screen) | The switch element |
- Each screen decides which states it writes, listed in a table in the screen document. Names not in the table can be written too (the mechanism doesn’t limit names).
Others
data-autofocus: the element that gets focus on opening. Without it, the screen’s outer frame gets focus (same as today’s screens).
What the engine adds to elements with attributes
In two steps.
- When a part is built (
renderPartintemplate.ts): adata-roleelement becomes a UI-part button (buttoninui-parts.ts):role="button"andtabindex, Enter and Space, not passing the click on to the stage, sound, and states such asdata-lm-hover. A<button>withouttypegetstype="button". - When the screen opens (
openStandardandpushScreenindefault.ts, same as plugin screens):- Clicks and wheel inside are not passed on to the stage.
role="dialog"(or analertdialogordialoginside, if there is one) andaria-modal. With a heading (.lm-screen-title,h1,h2),aria-labelledby.tabindex="-1"on the outer frame; focus moves inside on opening and back to the element from before on closing.- Tab and Shift+Tab cycle inside the screen. Arrow keys and the gamepad’s d-pad move to the nearest visible, selectable element in that direction. Gamepad A is Enter and B is Esc.
- Esc, right click and gamepad B close the screen (“No” on the confirmation).
- With text-to-speech on, the text of a button selected by keys is read.
- If the confirmation template has neither
dialognoralertdialogas arole, the outer frame becomesalertdialog(so it’s read as a confirmation even if the game forgot it).
The interface of a screen built from a template (code side)
What renderPart and ScreenTemplates.render return (TemplateView). Used by the code that builds screens (the built-in screens, and later an interface opened to plugins).
el: the outermost element of the part.autofocus: thedata-autofocuselement.items(list): the current rows of a list (the outermost element of each built part). Used to writedata-actionon the menu row of a game screen, and to tell recordings which elements of choices are pressable.list(list, row values): rebuilds a list while open (the interface for “screens whose contents change while open”, found in the prototype). If focus is in the list, it stays on the row with the same index after rebuilding (or the last row if that one is gone). Changing the tab of the controls help rebuilds the tabs and the body. Paging and saving in save and load (stage f2) and backlog search also rebuild with this.update(values): rewrites only the values of a part (added in stage f2: the text ofdata-fieldand the attributes ofdata-attr-*; only the given names change, list rows are not touched, and elements removed bydata-ifare not brought back). Used for the backlog search count. The value text of the settings screen’s sliders doesn’t useupdateand is written directly into itsdata-fieldelement (updaterewrites everydata-fieldelement of the part, which would wipe the preview text the engine puts in).- A
data-roleaction receives the pressed element (added in stage f3, so a tab button on the settings screen can tell whichdata-pageit is;api.pressin screen JS doesn’t pass it). - Initial focus that attributes can’t express is decided by the code that builds the screen. Choices focus “the first pressable choice” (as before;
data-autofocuson the row part would put it on every row). A saved position and the previously chosen slot (save and load, title) are decided by each screen’s code (stage f2’s save and load decides the page to open in code, and focus stays on the outer frame as before).
When the built-in templates are loaded
- The confirmation template and the template machinery (
template.ts) are in the bundle loaded before play starts (core). The confirmation must show right away even when bundles can’t be fetched (the connection dropped, an exported file is missing) or when asking about an update. It is small. - The templates of the menu, controls help, ending name, choices, save and load, backlog, title and settings screens are a separate bundle in exports (
engine/lazy-screens-<key>.js, entrysystem/lazy-screens.ts), loaded the first time one of them opens (loadScreenTemplatesinsystem/standard-templates.ts). Once loaded, the remaining built-in templates are added to the store (ScreenTemplates.addStandard). - The code that builds screens from templates (
menu-panel.ts,help.ts,ending.ts,choice-view.ts,saveload.ts,backlog-view.ts,title-view.ts,config.ts) is in the first bundle. Splitting it into another bundle made the total 0.14 KB larger (tried), because each bundle is minified separately. The first-bundle budget has room, but the total budget doesn’t.- The menu, controls help, save and load, backlog and settings open once loading finishes (opening the same screen again while loading happens only once). The ending name, choices and title wait for loading before showing. Only the first opening in an export is delayed, and only while loading. The title music plays without waiting.
- If loading fails: the menu, controls help, save and load, backlog and settings don’t open (they try again next time). The ending name is skipped and the game goes on to the title. Choices and the title can’t move on, so they retry every second (choices until the script stops them, the title while on the title).
- Choices are read aloud after they are laid out (checking every 80 ms, up to two seconds).
- The dev server (and Studio’s previews) load them up front (when
import.meta.hot; scripts are edited and reopened, so no waiting). Studio Mobile also loads them up front (studio-mobile/scripts/build.tscallsloadScreenTemplates). - Exports remove the indentation and line breaks between elements of the built-in templates (
squeezeScreenTemplatesinpackages/cli/src/strip-docs.ts). The player drops whitespace-only text containing line breaks on reading, so the screens built are the same. - Size (the template game, gzip;
packages/cli/test/engine-bundle.test.ts): before stage f1, what’s loaded before play was 225.0 KB, and the total 256.8 KB. After stage f1 (after taking in the screen JS interface), 226.9 KB and 259.6 KB (budgets 240 KB and 260 KB). The template text (about 1 KB) left the first bundle but counts in the total. Only 0.4 KB was left under the total budget, so exports remove template indentation, strip text only the dev server reads (template error messages, screen JS error messages), and removeScreenTemplates.stale, which the player doesn’t use. Before rewriting the remaining screens (save and load, backlog, title, settings), the total budget had to be revisited or other things made smaller (splitting into another bundle doesn’t lower the total). - Before stage f2, the user decided to raise the total budget to 270 KB (the 240 KB budget for what loads before play stays). Before stage f2 it was 226.9 KB and 259.7 KB, after it 227.4 KB and 260.9 KB. The three templates went into the separate bundle. The first bundle grew by 0.5 KB because the code building the three screens from templates and
view.updateare slightly larger than the previous screen code. - After stage f3 (settings), 226.7 KB and 261.6 KB. The first bundle shrank by 0.7 KB (building each item and looking up its wording moved into the template, and the code building from the template is smaller than the previous code). The total grew by 0.7 KB (the text of the settings template).
How the engine holds the built-in templates
- The template text is an
export constinpackages/screens/src/<screen>.ts, listed inSTANDARD_TEMPLATESinpackages/screens/src/index.ts.ScreenTemplatesintemplate.tsreads them (laying the game’s over the built-in ones, and remembering each part once read). - Reading
.htmlwith?rawis not used, so other packages that read the source of@liminalia/web(Studio, Studio Mobile, tests) can type-check without Vite’s types. Copying imports the same text and writes it into the game. - Export size (
packages/cli/test/engine-bundle.test.ts, the template game, gzip): before the prototype, what loads before play was 222.8 KB and the total 254.6 KB. After the prototype, 224.7 KB and 256.5 KB (budgets 240 KB and 260 KB). After stage f1, see “When the built-in templates are loaded” above. - Each screen’s rules (working
data-rolevalues, value names, requireddata-rolevalues, the plugin hooks that replace it) are written in that screen’s file as<screen>_SCREEN(StandardScreen) and listed inSTANDARD_SCREENSinpackages/screens/src/rules.ts(read by template checks, copying and the table indocs/screens.md; not in the player’s bundle). See “Checking templates” below.
How a game’s templates are registered and read
{ "ui": { "templates": { "confirm": "ui/screens/confirm/confirm.html" } } }
- In
ui.templatesofliminalia.json, write the screen name (idinscreen-catalog.ts) → the template file (a path from the game folder, whereliminalia.jsonis). The usual place isui/screens/<screen>/<screen>.html(copying puts it there). Other places work too. - The
langtype (UiConfig.templates), its check (checkUi;.html, a path inside the game folder) and the JSON Schema were updated together. - The dev server and exports read the file contents into the game information (
project.templatesofvirtual:liminalia/project;liveConfiginplayer-info.ts). The player doesn’t fetch the game’s files. Reasons: no 404 for missing files, export CSP and offline behave the same, and the confirmation can show right away without waiting. - The dev server watches the registered files and passes the contents with
liminalia:live-configon change. It takes effect from the next screen opened (an open screen is not rebuilt). The same applies whenui.templateschanges. - Files that can’t be read are not used (the dev server prints to the console). Missing files and unknown screen names are reported by
lim checkand the Studio check (template-missing,template-unknown-screen).
Keeping the original built-in version in a copied file
- Every built-in part has
data-version="version". When a built-in part changes, its version goes up by one (even for only added class names or attributes; anything games that copied it should know about). - Copying copies the part as is (including
data-version). The copied part’sdata-versionrecords “which built-in version it was copied from”. Creators don’t changedata-versionwhen editing. - If a game’s part is older than the built-in one, the part is reported (part name, copied version, current version). The player doesn’t report it (
stalePartsintemplate.tsstays for tests but isn’t in the export bundle).lim checkand the Studio check reporttemplate-outdatedand point tolim screens diff <screen>. - Showing the difference needs the original built-in text that was copied. Copying also writes the original text of each copied part into Studio’s store (
.liminalia/screens/<screen>/<part>.<version>.html). Comparing it with the current built-in template shows what changed in the built-in template, without mixing in the game’s edits (a three-way comparison). Without the store (copied elsewhere and so on), only the version numbers are reported. - The store is per part because versions go up per part. Keeping the whole screen’s text under one version can’t decide which original a game copied when it copied only one part, or copied parts at different times. A part’s
data-versionbecomes the store’s file name as is. - The comment at the top of the file (which engine version it was copied from) is for people; the engine doesn’t read it.
Replacing just one part
<template data-part="slot" data-version="1">
<li class="lm-slot my-slot">…</li>
</template>
- Only the parts written in a game’s template replace the built-in parts (merged per part name;
mergeTemplates). Withoutscreen, the whole screen stays built-in and only the row inside is the game’s. <style>is added to the built-in one.- Part names are treated as part of the interface. The built-in templates may add parts, but renaming or removing one breaks games that copied it, and is written in the CHANGELOG as action required.