Înregistrarea atributelor
Atributele sunt definite în tabloul blockstudio.attributes din fișierul block.json.
Exemplu de bază
JSON
{
"name": "my-theme/my-block",
"title": "My Block",
"blockstudio": {
"attributes": [
{
"id": "title",
"type": "text",
"label": "Title"
},
{
"id": "content",
"type": "textarea",
"label": "Content"
}
]
}
}
Proprietățile atributelor
Toate atributele au următoarele proprietăți:
| Proprietate | Tip | Descriere |
|---|---|---|
id |
string | Identificator unic al atributului |
type |
string | Tipul câmpului (text, textarea, select etc.) |
label |
string | Eticheta afișată în editor |
default |
mixed | Valoarea implicită a atributului |
help |
string | Text de ajutor afișat ca tooltip lângă etichetă |
position |
string | Locul în care apare câmpul: inspector (implicit) sau toolbar |
conditions |
array | Logica condițională pentru afișarea/ascunderea câmpului |
Poziție
În mod implicit, toate câmpurile sunt afișate în inspectorul blocului (bara laterală). Puteți schimba acest lucru folosind proprietatea position:
JSON
{
"blockstudio": {
"attributes": [
{
"id": "alignment",
"type": "select",
"label": "Alignment",
"position": "toolbar",
"options": [
{ "value": "left", "label": "Left" },
{ "value": "center", "label": "Center" },
{ "value": "right", "label": "Right" }
]
}
]
}
}
Valori implicite
Setați valorile implicite folosind proprietatea default:
JSON
{
"blockstudio": {
"attributes": [
{
"id": "title",
"type": "text",
"label": "Title",
"default": "Hello World"
},
{
"id": "showBorder",
"type": "toggle",
"label": "Show Border",
"default": true
}
]
}
}
Text de ajutor
Adăugați un text de ajutor pentru a îndruma utilizatorii:
JSON
{
"blockstudio": {
"attributes": [
{
"id": "apiKey",
"type": "text",
"label": "API Key",
"help": "Enter your API key from the settings page"
}
]
}
}
Accesarea atributelor
În șablon, atributele sunt disponibile prin prescurtarea $a (sau $attributes):
PHP
<h1><?php echo $a['title']; ?></h1>
<p><?php echo $a['content']; ?></p>
Sau în Twig:
Twig
<h1>{{ a.title }}</h1>
<p>{{ a.content }}</p>