Attribute registrieren
Attribute werden im Array blockstudio.attributes in deiner Datei block.json definiert.
Einfaches Beispiel
JSON
{
"name": "my-theme/my-block",
"title": "My Block",
"blockstudio": {
"attributes": [
{
"id": "title",
"type": "text",
"label": "Title"
},
{
"id": "content",
"type": "textarea",
"label": "Content"
}
]
}
}
Attributeigenschaften
Alle Attribute haben die folgenden Eigenschaften:
| Eigenschaft | Typ | Beschreibung |
|---|---|---|
id |
string | Eindeutige Kennung des Attributs |
type |
string | Der Feldtyp (text, textarea, select usw.) |
label |
string | Die im Editor angezeigte Bezeichnung |
default |
mixed | Standardwert des Attributs |
help |
string | Hilfetext, der als Tooltip neben der Bezeichnung angezeigt wird |
position |
string | Position des Felds: inspector (Standard) oder toolbar |
conditions |
array | Bedingte Logik zum Ein- oder Ausblenden des Felds |
Position
Standardmäßig werden alle Felder im Blockinspektor (Seitenleiste) angezeigt. Du kannst das über die Eigenschaft position ändern:
JSON
{
"blockstudio": {
"attributes": [
{
"id": "alignment",
"type": "select",
"label": "Alignment",
"position": "toolbar",
"options": [
{ "value": "left", "label": "Left" },
{ "value": "center", "label": "Center" },
{ "value": "right", "label": "Right" }
]
}
]
}
}
Standardwerte
Lege Standardwerte über die Eigenschaft default fest:
JSON
{
"blockstudio": {
"attributes": [
{
"id": "title",
"type": "text",
"label": "Title",
"default": "Hello World"
},
{
"id": "showBorder",
"type": "toggle",
"label": "Show Border",
"default": true
}
]
}
}
Hilfetext
Füge Hilfetext hinzu, um Benutzer anzuleiten:
JSON
{
"blockstudio": {
"attributes": [
{
"id": "apiKey",
"type": "text",
"label": "API Key",
"help": "Enter your API key from the settings page"
}
]
}
}
Auf Attribute zugreifen
In deinem Template sind Attribute über die Kurzschreibweise $a (oder $attributes) verfügbar:
PHP
<h1><?php echo $a['title']; ?></h1>
<p><?php echo $a['content']; ?></p>
Oder in Twig:
Twig
<h1>{{ a.title }}</h1>
<p>{{ a.content }}</p>