Mainstay GitHub
Content

Blocks are data, and Blade draws them

The hard requirement is that Mainstay stores data, not markup. That rules out writing blocks as HTML comments into an entry's content, rules out rendered HTML in a rich text field, and, read strictly, rules out Markdown, which is markup with a friendlier syntax. What is stored has to be something a program can read, check and draw again in a different design.

A block is a class, and a view

app/Blocks/Note.php
#[Icon('quote')]
#[Description('A line the reader should not miss.')]
#[Example(['text' => 'Mainstay is not released yet.'])]
class Note extends Block
{
    #[Textarea(required: true)]
    public string $text;
}
resources/views/blocks/note.blade.php
<div class="note">
    <p>@field($block, 'text')</p>
</div>

A block's fields are declared with the same attributes an entry's are, and checked, cast and described by the same field types. Its view is blocks. and its handle, and @field prints a text field escaped, the way {{ }} would. #[Icon], #[Description] and #[Example] are for the admin: how the block is listed, and what it is previewed with when an editor adds one.

A content type names the blocks its content may hold with #[Blocks([Hero::class, Features::class])] on its class. A block that holds blocks names them the same way, with text: true if paragraphs and headings may sit among them, and its view prints them with @inner($block).

What is stored

Every entry has a content: one document, in the JSON ProseMirror uses, whose top-level nodes are text and blocks. A block is a node of type block whose attributes are its id, its type and its data, with the blocks and text inside it as its content. The id is given when the block is first written and kept, which is how a block that moved is told apart from one that was replaced. Rich text inside a block is ProseMirror's JSON too: a tree of nodes, which is data.

Why not tables

A JSON column and relational tables both hold data, so that requirement alone did not decide it. Blocks could go in a table for each block type, with an order column and a parent key, or in a column as an ordered list of typed maps.

Two things Mainstay had already decided settled it. The schema is synced in development and migrated by hand in production, so tables for blocks would turn adding a field to a block into a schema change and a migration, during exactly the part of a project where blocks change most. And the constraints tables would buy go unused: the admin, the API and the site never filter a page's blocks in SQL. Top-level fields stay real columns, because a slug's uniqueness and an index on a date are used every day.

What it costs

  • Something in PHP walks the document and writes HTML at render time. Each block is drawn by its own view, and the rest by Mainstay.

  • The set of nodes is closed. A block whose type its field no longer lists is left out of a read, and out of the next save.

  • A question such as which pages use the pricing block cannot be asked in SQL. If it needs to be, an index derived from the content could answer it, without storing blocks in tables.

  • A reference stored inside a block has no foreign key behind it, which is why relations are read defensively.

Drawn by the site, edited on the page

Because the content is data and the site's views draw it, the admin can draw it the same way. Its Content tab is the page as the site renders it, from the form as it stands: text typed where it shows, blocks added and moved there, and a block's other fields edited in a dialog beside it. The decision was taken when the admin was expected to edit blocks as a list of forms; the editor changed, and the storage did not have to, beyond becoming one document of text and blocks instead of a list of blocks beside a body.

Keep reading

All articles