Sections and blocks
Write sections and theme blocks, declare their schemas, nest blocks, ship presets and use the core library.
Sections and blocks are what the merchant works with in the theme editor. A section is a band of a page; a block is a piece inside a section or inside another block.
A section
{# sections/content/image-with-text.twig #}
{% set s = section.settings %}
<section class="iwt iwt--{{ s.layout }}" {{ section.attributes }}>
<div class="iwt__media">
{% if s.image %}
{{ s.image|image_url(width: 1600)|image_tag(alt: s.image.alt, sizes: '(min-width: 900px) 50vw, 100vw') }}
{% else %}
{{ placeholder_svg('image', 'iwt__placeholder') }}
{% endif %}
</div>
<div class="iwt__text">{{ content_for('blocks') }}</div>
</section>
{% stylesheet %}
.iwt { display: grid; grid-template-columns: 1fr 1fr; gap: var(--gap, 24px); }
.iwt--image-right .iwt__media { order: 2; }
{% endstylesheet %}
{% schema %}
{
"name": "t:sections.image_with_text.name",
"settings": [
{ "type": "image_picker", "id": "image", "label": "t:sections.image_with_text.image" },
{ "type": "select", "id": "layout", "default": "image-left", "label": "t:sections.image_with_text.layout",
"options": [
{ "value": "image-left", "label": "t:sections.image_with_text.left" },
{ "value": "image-right", "label": "t:sections.image_with_text.right" }
] }
],
"blocks": [{ "type": "@theme" }],
"presets": [
{
"name": "t:sections.image_with_text.name",
"blocks": {
"heading": { "type": "text", "settings": { "text": "Made to last", "size": "xl" } },
"cta": { "type": "button", "settings": { "label": "Shop now", "url": "/categories/all/" } }
},
"block_order": ["heading", "cta"]
}
]
}
{% endschema %}
Three things every section does:
{{ section.attributes }}on the root element. The theme editor uses these attributes to select the section in the preview.- Settings through
section.settings, already resolved: animage_pickergives a file, aproductsetting a product. An empty setting isnull. - Blocks with
{{ content_for('blocks') }}, where they belong in your markup.
The schema
| Key | Description |
|---|---|
name |
Name in the editor, usually a t: key. |
settings |
The settings - see Settings and Setting types. |
blocks |
Which blocks the section accepts. A non-empty list is what tells the editor the section takes blocks at all - see below. |
presets |
Ready configurations the merchant picks from "Add section". A section without a preset can only be placed in templates by you. |
targets |
Where the block content editor offers the section or block: page, post, offer, email. Without it, it is a theme-only section. |
group |
A category in the "Add section" list, for example content. |
Blocks
Theme blocks live in blocks/ and must declare a schema. They render the same way as sections, with block instead of section:
{# blocks/text.twig #}
{% set size = block.settings.size|default('lg') %}
<p class="block-text block-text--{{ size }}" {{ block.attributes }}>{{ block.settings.text }}</p>
{% schema %}
{
"name": "t:blocks.text.name",
"settings": [
{ "type": "inline_richtext", "id": "text", "label": "t:blocks.text.text", "default": "Text" },
{ "type": "select", "id": "size", "default": "lg", "label": "t:blocks.text.size",
"options": [{ "value": "xl", "label": "XL" }, { "value": "lg", "label": "L" }, { "value": "md", "label": "M" }] }
]
}
{% endschema %}
Which blocks a section accepts
Entry in blocks |
Means |
|---|---|
{ "type": "@theme" } |
Any theme block in blocks/ (and the core blocks). |
{ "type": "@app" } |
Blocks from apps, when apps arrive. Declaring it now costs nothing. |
{ "type": "faq-item" } |
A block meant for this section. List several to describe a set. |
Nesting
A block can accept blocks too - declare blocks in its schema and print {{ content_for('blocks') }}. That is how layout blocks work: base ships _group, a flex container with direction, gap and alignment settings, and the merchant composes columns and rows from it without you writing each combination. Blocks nest up to four levels.
A block that is listed by name in a parent's blocks is a child block - it is meant to live inside that parent only (faq-item inside faq). Child blocks do not declare targets.
Private and system blocks
A file name starting with an underscore - _group.twig - marks a system block: available for nesting, but not offered as a standalone item in the editor's list.
Styles and scripts
{% stylesheet %} in a section or block is collected, with every other one in the theme, into a single _sections.css that the platform links in content_for_header. The CSS loads once, however many times the section appears, and is cached with the theme version.
{% javascript %} is collected into _sections.js the same way, but that file is not linked for you. Prefer a module in assets/ loaded from the layout; see Assets.
Style sections with the brand tokens (var(--color-accent), var(--radius)) and your own custom properties, not hard-coded colours - the merchant changes the tokens in theme settings and expects every section to follow.
Documenting snippets and blocks
{% doc %} holds the parameters a snippet expects. It never renders, but it is how the next developer - and the linter that is coming - knows how to call it:
{% doc %}
@param product - the product to show
@param show_rating - optional, show stars under the title
{% enddoc %}
The core library
The platform ships sections and blocks to every theme, under your files:
- Sections -
content,banner,media-text,callout,hero-tiles,product-list,term-grid,logo-wall,newsletter,testimonials,post-list,faq,contact-form. - Blocks -
heading,paragraph,image,button,list,quote,columns,divider,spacer,htmland the child blocks of the sections above.
They style themselves only with brand tokens, with fallbacks, so they look right in any theme. Your file with the same name replaces the core one: ship sections/faq.twig and the store uses yours, and the core CSS of faq is no longer loaded. Keep the setting ids of the core file when you do, so the merchant's content carries over. Settings and blocks of every core file are listed in Core sections and blocks.
When a merchant switches themes, a section type the new theme does not know becomes a placeholder that keeps its settings, and comes back when the type is available again. Content is never lost.
Data inside a section
A section gets the variables of the page it is on (product on an offer page, category on a category page) plus the globals. When a section needs data the page does not give it - bestsellers on the home page, posts on a product page - it asks for it with the data functions: query_products(), query_posts(), query_terms(), query_categories(). They always read the current store. See Functions.