Sections and blocks
Define sections and blocks with <hk-schema>: field types, how settings are coerced, template default sections and repeatable blocks.
On this page
A section is a page part a merchant adds, removes, reorders and configures:
a hero, a product grid, testimonials. A block is an item inside a section
that the merchant can add several of: one testimonial, one slide. Both are a
template plus a <hk-schema> that declares their settings.
A section
A hero: a dark band with a big heading, a sentence and a button. This is the smallest useful section.
<hk-section background="theme:secondary" padding-y="96" mobile-padding-y="48">
<hk-stack gap="16" align="start">
<hk-heading text="{{ section.settings.title }}" level="h1" size="56" mobile-size="32" color="#FFFFFF" />
<hk-text text="{{ section.settings.subtitle }}" size="20" color="#E5E7EB" />
<hk-button label="{{ t('shop_now') }}" href="/products" />
</hk-stack>
</hk-section>
<hk-schema>
{
"name": "Hero",
"version": 1,
"settings": {
"title": {
"type": "text",
"label": "Title",
"default": "Hello",
"maxLength": 80
},
"subtitle": {
"type": "textarea",
"label": "Subtitle",
"default": "",
"maxLength": 200
}
}
}
</hk-schema>What each line does
- Line 1
<hk-section>is the band.background="theme:secondary"paints it with the theme'ssecondarycolour fromtheme.json.padding-y="96"is the space above and below the content;mobile-padding-y="48"halves it on phones. - Line 2
<hk-stack>puts the heading, sentence and button in a column, 16 px apart, lined up at the start of the line. - Line 3
<hk-heading>is the title.{{ section.settings.title }}prints thetitlesetting: what the merchant typed, or the default. - Line 4
<hk-text>prints thesubtitlesetting. - Line 5
<hk-button>is a link drawn as a button.t('shop_now')takes its words fromlocales/en.json. - Lines 8-27
<hk-schema>declares the settings the merchant edits in the store builder.
Rules for the file:
- The file name is the section type:
hero. <hk-schema>is JSON (double quotes, no trailing commas; SN1107). Put it at the end of the file.nameis required (SN3030). It is what merchants see in the builder.versionis the schema version (a number, default 1).settingsis an object of fields. Read them assection.settings.<key>.- Inside a section you also have
section.id,section.typeandsection.blocks.
Field types
Every field is:
{ "type": "…", "label": "…", "default": …, "min": …, "max": …, "step": …,
"options": […], "maxLength": …, "required": …, "responsive": …, "visible_if": { "setting": "…", "equals": … } }Only type and label are needed. One example of each type:
| Type | Value in templates | Example field |
|---|---|---|
text | short string | "title": { "type": "text", "label": "Title", "default": "Summer sale", "maxLength": 80 } |
textarea | longer string | "body": { "type": "textarea", "label": "Body", "default": "" } |
rich_text | string | "story": { "type": "rich_text", "label": "Story", "default": "" } |
number | number | "padding": { "type": "number", "label": "Padding", "default": 64, "min": 0, "max": 240 } |
range | number (slider) | "count": { "type": "range", "label": "Count", "default": 8, "min": 2, "max": 24, "step": 1 } |
boolean | true / false | "show_title": { "type": "boolean", "label": "Show title", "default": true } |
select | one of options | "align": { "type": "select", "label": "Align", "options": ["start", "center", "end"], "default": "start" } |
color | #hex or theme:<token> | "background": { "type": "color", "label": "Background", "default": "theme:background" } |
image | image reference (URL string) | "image": { "type": "image", "label": "Image" } |
video | video reference | "video": { "type": "video", "label": "Video" } |
link | URL string | "link": { "type": "link", "label": "Link", "default": "/products" } |
icon | icon name | "icon": { "type": "icon", "label": "Icon", "default": "truck" } |
product | product handle | "product": { "type": "product", "label": "Product" } |
collection | collection handle | "collection": { "type": "collection", "label": "Collection", "default": "featured" } |
menu | menu handle | "menu": { "type": "menu", "label": "Menu", "default": "main" }: read it with menu(section.settings.menu) (see Menus) |
spacing | string | "gap": { "type": "spacing", "label": "Spacing", "default": "md" } |
typography | string | "font": { "type": "typography", "label": "Typography", "default": "body" } |
Notes:
productandcollectionstore a handle string.product()andcollection()take a literal handle or a setting of their own type:collection(section.settings.collection)reads the collection the merchant picked. Any other argument is SN1105 (see Data and expressions).required,visible_if(show this field only when another setting equals a value) andresponsiveare hints for the editor. Rendering does not enforcerequired; always set a sensibledefault.- Labels are shown to merchants in the dashboard: keep them short and plain.
How settings reach the page (coercion)
The merchant's stored values are never trusted as-is. Before a section renders, each declared setting is filled and coerced:
| Situation | Result |
|---|---|
| No stored value | default (or nothing, if there is no default) |
text, textarea, rich_text, image, video, link, icon, product, collection, menu, spacing, typography | must be a string, else default; cut to maxLength if set |
number, range | numbers and numeric strings are parsed and clamped to min/max; anything else gives default |
boolean | true, "true", "yes", 1 → true; false, "false", "no", 0 → false; else default |
select | must be one of options, else default |
color | must be # + 3-8 hex digits or theme:<token>, else default |
| Setting not declared in the schema | dropped; templates never see it |
So a stored padding of "999" with max: 240 renders as 240, and an align
of "diagonal" renders as the default "start". A bad value never breaks the
page.
Values written directly on hk-* tags are not coerced by your schema; each tag
checks its own props (a size outside its range is clamped, an unknown variant
falls back to the tag default). See the tag reference.
Template default sections
A template's <hk-schema> lists the sections a new page starts with, in order:
<hk-sections />
<hk-schema>{"sections":[
{"type":"hero","settings":{"title":"Welcome","subtitle":"A theme built with Sneferu"}},
{"type":"featured","settings":{"title":"Featured products"}}
]}</hk-schema>typemust be a file insections/(SN3020); each entry is{ "type", "settings"? }(SN3026).settingshere go through the same coercion as stored values.- A default section may also list
blocks(see below). They are whatsneferu devshows. - Once a merchant edits the page, their saved list of sections replaces the defaults.
Default blocks
A default section can start with blocks. Each entry is { "type", "settings"? }:
<hk-sections />
<hk-schema>{"sections":[
{"type":"faq","settings":{"title":"Questions"},"blocks":[
{"type":"question","settings":{"text":"How long does shipping take?"}},
{"type":"question","settings":{"text":"Can I return an item?"}}
]}
]}</hk-schema>- Each
typemust be a file inblocks/(SN3028). - The section must allow the type in
blocks.allowed, and the list may not be longer thanblocks.max(SN3029). - The blocks render with the ids
default-<section index>-<block index>, for exampledefault-0-1.
Text in two languages
Add "localized": true to a text, textarea or rich_text setting and the
merchant can store one value per language:
"title": {
"type": "text",
"label": "Title",
"localized": true,
"default": { "ar": "أهلًا بك", "en": "Welcome" }
}Templates still read {{ section.settings.title }} and get a plain string: the
value for the page's language, else the other language, else the default, else
an empty string. A plain string saved by an older editor is used as it is in
both languages. maxLength applies to the resolved string. Theme-level
settings in theme.json can be localized the same way.
Blocks
A customer quotes section: the merchant adds as many quotes as they like, up to six, and edits each one. Each quote is a block.
<hk-card border="true" padding="20" radius="16">
<hk-stack gap="10">
<p class="quote">{{ block.settings.text }}</p>
<hk-text text="{{ block.settings.author }}" size="14" color="#4B5563" />
</hk-stack>
</hk-card>
<hk-schema>
{
"name": "Quote",
"version": 1,
"settings": {
"text": {
"type": "textarea",
"label": "Quote",
"default": "Delivery was fast and the packaging was perfect.",
"maxLength": 280
},
"author": {
"type": "text",
"label": "Name",
"default": "Mona, Alexandria",
"maxLength": 60
}
}
}
</hk-schema><hk-section padding-y="64" mobile-padding-y="40">
<hk-stack gap="24">
<hk-heading text="{{ section.settings.heading }}" level="h2" size="32" mobile-size="24" />
{% if section.blocks %}
<hk-grid columns="3" mobile-columns="1" gap="16">
{% for block in section.blocks %}
<hk-block of="{{ block }}" />
{% endfor %}
</hk-grid>
{% else %}
<hk-text text="{{ t('no_quotes_yet') }}" color="#4B5563" />
{% endif %}
</hk-stack>
</hk-section>
<hk-schema>
{
"name": "Testimonials",
"version": 1,
"settings": {
"heading": {
"type": "text",
"label": "Heading",
"default": "What our customers say"
}
},
"blocks": {
"allowed": ["quote"],
"max": 6
},
"presets": [
{
"name": "Three quotes",
"blocks": [
{
"type": "quote"
},
{
"type": "quote"
},
{
"type": "quote"
}
]
}
]
}
</hk-schema>.quote { margin: 0; font-size: 17px; line-height: 1.6; }What each line does
In blocks/quote.snf:
- Lines 1-6 one quote: a bordered card with the quote and the customer's
name. A block reads its own settings as
block.settings.<key>. - Lines 7-26 the two settings of one quote: the text and the name.
In sections/testimonials.snf:
- Line 4
{% if section.blocks %}is true when the merchant has added at least one quote. - Lines 6-8
{% for block in section.blocks %}goes through the quotes in the merchant's order, and<hk-block of="{{ block }}" />draws each one withblocks/quote.snf. - Lines 10-11 what shows when there are no quotes yet.
- Lines 26-29
"blocks"tells the builder which block types this section takes (quote) and how many (6 at most). - Lines 30-45
"presets": when the merchant adds this section, the builder offers to start it with three quotes.
How it works:
section.blocksis the list of the merchant's blocks in order, hidden ones removed. Each hasid,typeand coercedsettings.<hk-block of="{{ block }}" />rendersblocks/<type>.snfwithblockin scope, wrapped in<div data-hk-block="<id>">. The section's scope (section,store, …) stays visible.- A block whose type is not in the theme is skipped with SN4012; the rest of the section renders.
blocks.allowed(which block types the editor offers) andblocks.maxare read by the editor. The platform also caps a section at 50 blocks and a page at 60 sections.presetsare starting configurations the editor offers when the section is added. They are stored with the theme and used by the builder; the CLI does not render them.- Because default sections cannot carry blocks,
sneferu devshows a block section with no blocks. Design that state, as lines 10-11 do: it is also what a merchant sees right after adding the section.
Try it
- Save the three files and add the key to
locales/en.json:"no_quotes_yet": "No quotes yet. Add one in the editor.". - Add
{"type":"testimonials"}totemplates/index.snf. - Run
npx sneferu validateand look at the preview: you see the empty state.
Snippets vs blocks
| Use | When |
|---|---|
| Block | The merchant decides how many there are and edits each one |
Snippet ({% render %}) | You repeat markup in code, with values you pass in |
{% render 'price-tag', amount: p.price %} renders snippets/price-tag.snf
with amount in scope, on top of the caller's scope. Snippets nest up to 8 deep
and may not render each other in a cycle (SN3027).
