Repository navigation
Description
Problem
Some apps want people other than the developer to shape pages: a CMS editor, a customer building a landing page, a user laying out a dashboard. The tempting way is to let them write component templates. A template is code, though: it can reach any value the component passes it, and keeping a template language safe for untrusted authors is a hard, never-finished job. Citry should make the safe way the easy way, so nobody needs to hand template writing to third parties.
Proposal: blocks, composed as data
The developer defines a fixed set of blocks. Anyone else composes pages from those blocks, and sends the result as data (JSON), never as template text. This is how Notion's blocks work.
- The developer declares each block:
- its kind;
- how it renders, either as a Citry component or as a plain function;
- the props it accepts, as a schema, which is validated on the server;
- metadata: whether it can hold children, which block kinds may appear inside it, whether it can repeat, and min/max counts.
- The developer declares the hierarchy: which blocks can sit at the root, and which can sit inside which. Citry checks every document against it.
- The client sends a document, a tree of
{kind, props, children}nodes. The server validates it against the declarations, then renders it with the developer's components. Unknown kinds, props that aren't declared, and children that aren't allowed are refused.
What the library would provide
- The browser side: helpers to build and edit a block document (insert, move, delete, and check against the allowed hierarchy before sending), and to serialize it. The library might also ship a headless editor component in Citry UI.
- The server side: parse and validate a document, render it to HTML (and back to JSON for editing), and report errors as field paths, like Events validation does.
- Declarations: a Python API to declare blocks and the hierarchy, and a way to share that declaration with the browser so both sides agree on what is allowed.
- Versioning: a stored document names its schema version, so an app can migrate old documents when blocks change.
Then change the guidance
Once this exists, Citry's docs should strongly discourage letting third parties write component templates. Only the developer writes components and templates, and anyone else composes the allowed blocks. The expression sandbox would stay as defense in depth, not as a way to accept templates from untrusted authors.
Error modes to design
- A document with an unknown block kind, a prop that isn't declared, a wrong type, or a child that isn't allowed is refused, with field-path errors.
- A document that is too deep or too large is refused, with limits the developer can set.
- A stored document from an older block schema either migrates or reports which blocks no longer exist.
Related
- Form integration: generate UI and validation from Python classes #129 (forms from Python classes) is a similar "declare once, render safely" feature, with similar priority.
- The expression sandbox docs (
docs_site/content/security.md, "Sandbox template code").
Activity