Editor Elements
An app can add its own elements to the SitePack page builder. The merchant drags one onto a page, a blog post or a category like any built-in element, fills in the settings you declare, and the storefront renders your Twig template in that spot.
Think of a financing calculator where the merchant picks bikes or scooters, a booking widget for one specific service, or a list of your app's records filtered one way on one page and another way on the next. Anything your app shows in the content of a page, at a place the merchant chooses, is an element. Anything it shows on every page at a fixed spot is a storefront block.
Declaring an element (elements)
{
"elements": [
{
"key": "financing",
"name": "elements.financing.name",
"description": "elements.financing.description",
"icon": "fas fa-hand-holding-usd",
"template": "financing.twig",
"settings": [
{
"key": "type",
"type": "options",
"label": "elements.financing.settings.type.label",
"options": [
{"value": "bike", "label": "elements.financing.settings.type.options.bike"},
{"value": "scooter", "label": "elements.financing.settings.type.options.scooter"}
],
"default": "bike"
},
{"key": "heading", "type": "text", "label": "elements.financing.settings.heading.label"}
]
}
]
}
| Key | Required | Description |
|---|---|---|
key | yes | How the page stores which element it is: lowercase letters, digits and dashes, at most 50 characters. Keep it stable: elements merchants already placed refer to it. |
name | yes | The element's name in the editor's Apps group, the "+" picker and the settings popover. |
description | no | A sentence at the top of the element's settings. |
icon | no | A Font Awesome class, such as "fas fa-bicycle". Defaults to a puzzle piece. |
template | yes | The Twig template under templates/ the storefront renders for the element. |
settings | no | What the merchant fills in per element, in the order the form shows them. See below. |
name, description and every setting label, help and option label can be a translation key: the editor shows them in the merchant's language.
Settings
Settings use the same field types as post type fields, except gallery and post:
type | Editor input | Value in Twig |
|---|---|---|
text | One-line text | string or null |
textarea | Multi-line text | string or null (print with nl2br) |
number | Number | integer, float or null |
image | Media library picker | absolute URL of the image, or null |
url | Text | a https:// address, a path (/contact), #anchor, mailto: or tel:, or null |
email | string or null | |
date | Date picker | YYYY-MM-DD or null |
boolean | Switch | true / false |
options | Select | the chosen option's value |
Each setting takes key, type, label, and optionally help (a hint under the input), required: true, default, and for options an options list of strings or {"value": "...", "label": "..."} objects.
A new element starts with every setting on its default. An options setting without a valid default starts on its first option, so it always has a value.
The template
The template is a fragment, rendered in your app's own sandbox, like your blocks: the same allow-list of tags and filters and your own translations/. It gets:
| Variable | What it is |
|---|---|
element | The merchant's values, by setting key: element.type, element.heading. Every declared setting is present. |
element_id | An id that is unique on the page, such as sp-app-element-12, for id and for attributes. |
settings | Your app's settings for the site, in the page's language, as in a block. |
app, config, site | As in a block: the installed app, your app.json and the site. |
{# templates/financing.twig #}
<section class="myapp-financing" id="{{ element_id }}">
{% if element.heading %}<h2>{{ element.heading }}</h2>{% endif %}
<div data-myapp-financing data-dealer="{{ settings.dealer_code }}" data-type="{{ element.type }}"></div>
</section>
The storefront wraps the output in <div class="sp-app-element" data-sitepack-app="<uuid>" data-app-element="<key>">. Style it from a stylesheet your app registers with styles and prefix your class names. Anything interactive goes in one of your scripts: they load on every page, so let them look for your element's markup and do nothing when it is not there.
What SitePack checks
The values your template gets are always the declared type, so the template never has to guess:
- A value the merchant never saw, because you added the setting in a later version, is on its
default. - A value that does not fit its type is
null: a number field holding text, aurlthat is ajavascript:address. Anoptionsvalue that is not one of your options falls back to the default, or the first option. - A value the merchant emptied stays empty (
null), the way the editor shows it. - Keys you do not declare are left out.
Text the merchant types is escaped when you print it, like everything in an app template.
When the element renders nothing
The element renders nothing, without breaking the page, when:
- your app is no longer installed on the site. The editor then shows the element as belonging to an app that is not installed, so the merchant can remove it;
- the version of your app the site runs has no element with that
key, or its template is missing; - your template throws an error. The error is logged.
In the editor
The editor lists your elements in an Apps group at the bottom of the element palette, and in the "+" picker on the canvas. The canvas does not run your template: it shows a card with your icon, the element's name and the first settings the merchant chose. The storefront and the page preview show the real thing.
Review
The review checklist has an Editor elements check. It fails when an element cannot be used as declared (a missing name, a key or setting key in the wrong format, an unknown setting type, an options setting without options) or when its template is not in templates/.
Next steps
- App Manifest (app.json): where
elementssits next to your blocks, scripts and styles. - Storefront Blocks & Events: for what your app shows on every page.
- Translations: translate your element's name and settings.