# dhtml: guide for AI agents and for people in a hurry
Read this first. It is short on purpose and covers what most apps need. The full references are [syntax.md](syntax.md), [templates.md](templates.md) and [config.md](config.md). **Never invent a tag, attribute or filter that is not listed there**: the check that runs on every upload will refuse it, and the error names the file and line.
## The loop
dhtml sites run on dhtml.net. There is nothing to install and no local server: you write the files of the project, zip them and upload them to your site.
```text
# write dhtml.config, server/migrations/001_*.sql, pages, actions, layouts, public/
# zip the CONTENTS of the project folder (dhtml.config at the top of the zip; no .env, no server/data)
# upload the zip: the Deploy page of your site on dhtml.net, or the deploy API
# read the result; fix every error, read every hint, upload again
```
Every upload is checked before it goes live: templates, routes, config and migrations are compiled, the database is backed up, and migrations are applied. A check that fails says the file, the line and a hint, and **the live version of the site is left as it was**. Upload after every few files, not at the end. Secrets (API keys, passwords) are not in the zip: they are set in the Secrets section of the site's page on dhtml.net and are named `env:NAME` in `dhtml.config`. To deploy from a script or an AI assistant, use a deploy key: see [dhtml.net/ai/deploy-api.txt](https://dhtml.net/ai/deploy-api.txt).
## Files
| Where | What |
|---|---|
| `server/pages/**/*.dhtml` | GET pages. The path is the URL: `blog/[slug].dhtml` is `/blog/:slug`, read as `{{ route.slug }}` |
| `server/api/*.dhtml` | Actions (forms, JSON). `server/api/save.dhtml` is `POST /api/save` |
| `server/layouts/` `partials/` `components/` `steps/` | Shell with ``; fragments (``); pieces with inputs (``, `...`, read as `props.a`); **steps** (``: queries, checks and guards written once, used in pages, actions and jobs) |
| `server/jobs/*.dhtml` | Work later or by the clock |
| `server/migrations/NNN_name.sql` | Plain SQL, applied in order. Put sample/seed rows here |
| `public/` | Static files (CSS, images, JS). Served as they are |
| `dhtml.config` | Settings (INI). Secrets are set in the Secrets section of the site on dhtml.net and are named `env:NAME` |
## Values and logic
```html
{{ post.title }} {{ post.created | date }} {{ body | truncate 80 }} {{ user.name | default "Guest" }}
... ... {{ p.title }} Nothing yet. signed in not admins visitors
```
- Every `{{ }}` is escaped for where it lands. There is no raw output. `markdown` and `json` are the only filters that make HTML or script, and they must be last.
- Filters: `date upper lower default truncate slug markdown json first count add sub mul div round fixed thousands sha256 base64 urlencode`. Prices: `{{ cents | div 100 | fixed 2 }}` shows `12.50`; `{{ total | fixed 2 | thousands }}` shows `1,234,567.50`; `{{ d | date "02 Jan 2006" }}` takes a layout in quotes. **A `test` cannot use a filter** (`test="rows | count"` is an error): test the value itself, because an empty list, empty text, `0` and nothing are all false (`test="rows"`), or compare with `== != < > <= >=`. Also `not`, `and`, `or`; no parentheses. **Strings in a test use double quotes**, so write the attribute with single quotes: `test='b.status == "booked"'`.
- There is no number or date formatting filter. Format in SQL: `printf('%.2f', price_cents / 100.0)`, `strftime('%d.%m.%Y', date)`.
- Built-in names: `app route query form errors user page config csrf flash`.
- **`` tags can never sit inside an HTML tag or attribute.** So there is no "selected" helper: write two whole elements, one in each branch:
```html
```
## Pages read data
```html
SELECT id, title, slug FROM posts WHERE published = 1 ORDER BY id DESC LIMIT 20 SELECT * FROM posts WHERE slug = {{ route.slug }}
{{ post.title }}
```
- **Do not copy a rule between files.** Put the query that finds the shop, the settings every page shows, or a check like "this person belongs to this shop" in `server/steps/name.dhtml` and write `` where it applies (page top level, or among the steps of an action after ``). A steps file holds only steps, reads the same variables as its user, and can end the request with `` or ``.
- Queries go at the **top level of a page** (not inside ``, ``, a layout or a partial). In an **action**, queries, writes and mails may sit inside ``. Pass results to a layout as attributes (`page.title`).
- Each `{{ }}` in SQL is a bound value: one plain value, **no filters**, never a table or column name, never after `FROM`/`JOIN`/`ORDER BY`. **An empty value is NULL in SQL, not `''`**: a missing `?date=` makes `{{ query.date }} = ''` match nothing. Write `COALESCE({{ query.date }}, '') = ''` or `{{ query.date }} IS NULL`; The check warns about the comparison with `''`. Build patterns in SQL: `LIKE '%' || {{ query.q }} || '%'`. Use `AS` when two columns share a name. Max 10000 rows.
- A layout sees only built-ins and `page.*`, not the page's variables, and cannot run a query: something every page shows (the site name) is queried at the top of each page and passed with ``.
## Actions write data
```html
INSERT INTO posts (title, body, user_id) VALUES ({{ form.title }}, {{ form.body }}, {{ user.id }})