Theme blocks
Read custom-theme blocks with typed fields or flat data, and expand reusable sections
A theme block is a block from a custom Liquid theme. On a site that uses a custom theme, the blocks field of a page or other record returns ThemeBlock items. Map each block type to a component in your front end.
To check the kind of site, read site.renderMode. It is theme for a custom-theme site and noCode for a website builder site.
query HomeBlocks {
page(path: "/") {
title
blocks {
__typename
... on ThemeBlock {
id
type
anchorId
data
}
}
}
}{
"data": {
"page": {
"title": "Home",
"blocks": [
{
"__typename": "ThemeBlock",
"id": "6afccddd00301f5343253233",
"type": "text-content",
"anchorId": null,
"data": {
"title": "Welcome to Season 2027",
"multi_column": false,
"content": true,
"content_html": "<p>Twenty-two concerts over the next two months.</p><p>Book early for the opening gala.</p>",
"theme_color": ""
}
}
]
}
}
}blocks returns only the blocks that editors left visible, in page order.
Block fields
| Field | Description |
|---|---|
type | The block name in the theme, for example hero or text-content. |
id | A stable ID for the block. Use it as a key in lists. |
anchorId | The anchor the editor set, for id attributes and in-page links. |
data | The block settings as one JSON object, in the shape that Liquid templates read as block. |
fields, field(key) | The block settings as typed fields. |
blocks | For a reusable section block, the blocks of the section. Empty for other blocks. |
Choose data or fields
Use data to port a Liquid template. The keys and values match the block object in Liquid. A rich text setting has two keys: the setting key and <key>_html with the HTML. data does not follow references. A setting that links to records does not hold those records.
Use fields when a setting links to other records, or when you want typed values. A reference setting returns the linked records, and you choose their fields in the query:
fragment FieldValue on Field {
__typename
key
type
... on TextField { value }
... on NumberField { number }
... on BooleanField { boolean }
... on SelectField { value values }
... on RichTextField { richText { html } }
... on MediaField { media { url alt } }
... on ReferenceField {
references {
__typename
... on Event { title path startDate }
... on Season { title path }
... on CustomObject { type title }
}
}
}
query HomeBlockFields {
page(path: "/") {
blocks {
... on ThemeBlock {
type
fields {
...FieldValue
... on GroupField { fields { ...FieldValue } }
... on ListField { items { fields { ...FieldValue } } }
}
}
}
}
}To read one setting, use field(key:):
query BlockTitles {
page(path: "/") {
blocks {
... on ThemeBlock {
type
title: field(key: "title") { ... on TextField { value } }
}
}
}
}Reusable sections
An editor can place a reusable section on a page. It appears as a theme block with the type reusableContentBlock. Its blocks field returns the blocks of the section:
query HomeSections {
page(path: "/") {
blocks {
... on ThemeBlock {
type
blocks {
... on ThemeBlock {
type
data
}
}
}
}
}
}A section can contain another section, up to four levels deep. To read a section by its ID, use reusableSection(id:).
Map blocks to components
Keep one component for each block type, and a fallback for types that your front end does not know yet:
type Block = { type: string; data: Record<string, unknown>; blocks: Block[] };
const components: Record<string, (data: Record<string, unknown>) => string> = {
"text-content": data => `<section><h2>${data.title ?? ""}</h2>${data.content_html ?? ""}</section>`,
};
function renderBlocks(blocks: Block[]): string {
return blocks
.map((block) => {
if (block.type === "reusableContentBlock") {
return renderBlocks(block.blocks);
}
return components[block.type]?.(block.data) ?? "";
})
.join("");
}Treat data values that come from editors as untrusted when you insert them into HTML. Rich text _html values are HTML that Basker produced.
Settings outside blocks
templateSettingson a record: the settings of the theme template that the editor chose.templatenames the template.site.settings: the global theme settings as fields.site.settingsJsongives the same values as one object, in the shape that Liquid templates read assettings.
Related
- Content model
- Custom objects and attributes
- Blocks
- Block schema: how a theme defines block settings.