Templates
A .pam.php file holds one component: the PHP class with its state and actions, a <template> of tags and a <style> block. Templates read like HTML, compile to the same immutable elements as a PHP render() method, and are checked at compile time, so a misspelled attribute is an error with a file and line instead of a silent no-op. pam desktop init writes one for the home screen.
A component
Section titled “A component”<?php
declare(strict_types=1);
namespace App\Screens\Inbox;
use Pam\Desktop\Attributes\State;use Pam\Desktop\Component;
final class InboxScreen extends Component{ public string $title = 'Inbox';
#[State] public int $count = 0;
public function increment(): void { $this->count++; }}?>
<template> <Column class="inbox"> <Text class="title">{{ $title }}</Text> <Button label="Add" on:press="increment" /> <Text p-if="$count > 0">{{ $count }} items</Text> </Column></template>
<style scoped> .inbox { flex: 1; padding: 24px; gap: 12px; } .title { font-size: 28px; font-weight: 700; }</style>Register the directory once in index.php. Every class found becomes a tag that other templates use by its short name (<InboxScreen />):
App::components(__DIR__.'/src', __DIR__.'/.pam-desktop/components');Syntax
Section titled “Syntax”| Syntax | Meaning |
|---|---|
{{ $expr }} |
Text from an expression. |
name="literal" |
A literal attribute. |
:name="$expr" |
An attribute bound to an expression. |
on:press="method" |
An event handler: a public method of the component, or an expression. |
p-if, p-else-if, p-else |
Conditional elements. |
p-for="$item in $items" with p-key="..." |
A repeated element with a stable key. The position is $itemIndex; rename it with p-index="i" or p-for="($item, $i) in $items". |
class="a b", :class="..." |
Classes of the scoped stylesheet. |
model="property" |
Two-way binding of an Input to a property. |
<Column p-for="$order in $orders" p-key="$order->id" class="order"> <Text class="name">{{ $order->customer }}</Text> <Text p-if="$order->paid" class="paid">Paid</Text> <Text p-else class="due">Due {{ $order->dueDate }}</Text></Column>Expressions
Section titled “Expressions”Expressions are a safe subset of PHP evaluated by the framework, never eval(). They can read:
- the component’s properties (
$title), loop variables and array indexes ($order['status']); - properties of objects (
$order->status); - the component’s public methods (
total($order)); - public instance methods of values (
$order->status->label()); - enum cases,
->valueand class constants (\App\Orders\Kind::Order->value); - a few PHP helpers:
count,in_array,trim,substr,min,max,array_sliceand others.
<Text>{{ $order->status->label() }}</Text><Text p-if="\App\Orders\Kind::Tab === $kind">Open tab</Text><VirtualizedList :maxHeight="min(440, count($rows) * 66)" /><Row p-for="$row in array_slice($rows, 0, 5)" p-key="$row->id">…</Row>Private, protected, static and magic methods, closures and calls on a string are refused when the template compiles or runs. ?:, ??, || and && short-circuit as in PHP, so $row->id === null ? 'No link' : $names[$row->id] never reads the missing index. Inside ??, a call on a missing value gives the fallback.
Events
Section titled “Events”A handler is a method name, which receives the event as its typed parameter, or an expression that uses $event:
<Button label="Save" on:press="save" /><Pressable p-for="$row in $rows" p-key="$row->id" on:press="pick($row, $event)">…</Pressable>$event is a PressEvent for presses, a KeyEvent for keys, a LayoutEvent for on:layout, a GestureEvent or GestureSettleEvent for gestures and a DragPayload for drops. Events without a payload class pass their raw value, such as the text of an input’s on:change.
Component events
Section titled “Component events”A component tells its owner that something happened with $this->emit():
final class Explorer extends Component{ public function choose(string $path): void { $this->emit('open', $path); }}The owner listens on the tag with on:name (or @name). The handler receives the payload, typed like any other handler:
<Explorer :workspace="$workspace" on:open="openFile" />On a component tag, every on: attribute is one of the component’s own events, even when the name matches a native event.
<Slot /> renders the children the parent passed. <Slot :content="$expr"> renders a value the component holds instead: a component, an element or anything renderable. When the value is null, the slot’s children are the fallback:
<Slot :content="$chart"> <Text>No data yet</Text></Slot>A component given through :content is mounted while the slot shows it and unmounted when the value changes. This is how a screen places a chart, a DesktopUI control or a dialog it keeps in a property.
Attributes worth knowing
Section titled “Attributes worth knowing”width,heightand theirmin/maxvariants take points or a percentage (width="75.5%"). A bound value may switch between the two.- A bound attribute that evaluates to
nullleaves its property unset, so:textColor="$active ? '#FFF' : null"falls back to the stylesheet. - Color attributes take
#rrggbb,#aarrggbb(alpha first, like the PHP integers) and any CSS color. Stylesheets read eight hex digits the CSS way, alpha last. When in doubt writergba(). testId="..."gives an element a stable identifier for tests and end-to-end scripts.:contextMenu="menu($row)"attaches a context menu.captureKeys="ArrowUp,ArrowDown,Enter"routes those keys to the element’son:keyDownbefore the focused native control sees them.
Compiling
Section titled “Compiling”Templates compile to PHP classes in the cache directory given to App::components(), once per change of the source.
- One component at a time. A component that does not compile is reported as a boot error with its file and line; every other component keeps working.
__DIR__and__FILE__in a.pam.phpfile name the source file, not the compiled class.- Read-only cache. When the cache directory cannot be written (an installed package), the compiler falls back to a private per-user cache.
- Bundles.
pam desktop buildprecompiles every template into the bundle, so an installed application does not compile at first launch.
Diagnostics
Section titled “Diagnostics”Each template is checked against the tags it uses. An unknown attribute, an event a tag never emits, or a prop a component does not declare is an error:
PAM2401 unknown attribute Text.color, did you mean textColor? at src/Screens/Card.pam.php:12:5.Run every check without opening a window:
pam desktop checkIn pam desktop dev the error overlay shows template errors at the template line and column, and stack frames in the compiled cache are mapped back to your .pam.php source. See Debugging.
Next steps
Section titled “Next steps”- Styling for the
<style>block. - State & Lifecycle for props, state and hooks.