Flow blocks

A flow field holds an ordered list of typed blocks, each with its own block model and template, for pages built from sections rather than one body.

A flow field holds blocks: a text section, then an image, then a quote. Each kind of block has a block model in flowblocks/, declaring its fields, and a template that renders one block.

Block models

A block model is flowblocks/<id>.toml. Its ID follows the rules for model IDs (motabhai::flowblock::invalid_id). It has an optional [block] table and [fields.*] tables like a page model's:

# flowblocks/text.toml
[block]
label = "Text"                 # defaults to the humanised ID

[fields.text]
type = "markdown"
required = true

[fields.style]
type = "select"
choices = ["plain", "note"]
default = "plain"
# flowblocks/image.toml
[block]
label = "Image"
template = "blocks/image.html" # the default: blocks/<id>.html

[fields.image]
type = "string"
label = "Attachment"
required = true

[fields.caption]
type = "string"
Key of [block]DefaultMeaning
labelthe humanised IDThe name shown in the admin's block editor.
templateblocks/<id>.htmlThe template one block renders with. It must not be empty (motabhai::model::empty_template).

A block has no body field (motabhai::flowblock::body_field) and no flow fields of its own, since blocks do not nest (motabhai::flowblock::nested_flow). The names reserved in page models, such as children, are free for a block's fields.

The flow field

A page model declares a field of type flow. blocks limits which block models it takes; without it, every block model is allowed. A flow field has no default (motabhai::model::invalid_default).

# models/page.toml
[fields.title]
type = "string"

[fields.content]
type = "flow"
blocks = ["text", "image"]

A block model that blocks names must exist (motabhai::model::unknown_block), and only flow fields take blocks (motabhai::model::unexpected_blocks).

Writing blocks in front matter

Each block is a TOML table in an array of tables named after the field. _block names its block model; the other keys are its fields.

+++
title = "Home"

[[content]]
_block = "text"
text = "Hello *there*."

[[content]]
_block = "image"
image = "hero.png"
caption = "A hero"

[[content]]
_block = "text"
text = "Bye."
style = "note"
+++

Because array-of-tables headers start a new table, put plain fields such as title before the first [[content]]. Blocks can also be inline tables:

content = [{ _block = "image", image = "a.png" }]

Blocks are checked like pages, and errors point into the block:

  • no _block: motabhai::content::missing_block_type;
  • a _block that is not a block model: motabhai::content::unknown_block;
  • a block model the field does not allow: motabhai::content::block_not_allowed;
  • a key starting with _ other than _block: motabhai::content::unknown_system_field;
  • a field that is not an array, a block that is not a table, or a _block that is not a string: motabhai::field::wrong_type;
  • missing required fields, unknown fields and invalid values, with the same codes as for pages.

A block gets the defaults of its block model for the fields it leaves out.

In the admin UI, a flow field is edited with a block editor that adds, removes and reorders blocks.

Templates

A flow field printed in a template renders every block with its template, one after another:

<h1>{{ this.title }}</h1>
{{ this.content }}

In a block template, this is the block, with _block and its fields, and record is the page the block belongs to. A field the block leaves out, with no default, is none:

{# templates/blocks/text.html #}
<div class="{{ this.style }}">{{ this.text }}</div>
{# templates/blocks/image.html #}
<figure>
  <img src="{{ this.image|url }}" alt="">
  {% if this.caption %}<figcaption>{{ this.caption }}</figcaption>{% endif %}
</figure>

Links in a block, whether from url or in Markdown, resolve against the page, so this.image|url above links to the page's attachment.

To control the markup around each block, iterate the field. Each item prints as that block rendered, and its fields can be read too:

{% for block in this.content %}
  <section class="block-{{ block._block }}">{{ block }}</section>
{% endfor %}

this.content.blocks is the same list, this.content|length counts the blocks, and a field with no blocks is false. On a page that leaves the field out altogether it is none, which also prints as nothing and is false, but has no blocks or length; test it with {% if this.content %} first.

A block template's errors point into that template. A page depends on the templates of the blocks it shows, so editing one rebuilds just those pages; editing a block model rebuilds the whole site.

Limits

  • Blocks cannot nest, and a block has no attachments of its own; an image block names one of its page's attachments, as above.
  • A block template can print a flow field of record, but not without end: block templates nested more than 8 deep are motabhai::template::block.
  • In a translation, a flow field is all or nothing: if index.de.md sets it, its blocks replace the primary language's blocks.
  • Atom feeds cannot use a flow field as entry content.