Attribuuttien rekisteröinti
Attribuutit määritellään block.json-tiedoston taulukossa blockstudio.attributes.
Perusesimerkki
JSON
{
"name": "my-theme/my-block",
"title": "My Block",
"blockstudio": {
"attributes": [
{
"id": "title",
"type": "text",
"label": "Title"
},
{
"id": "content",
"type": "textarea",
"label": "Content"
}
]
}
}
Attribuutin ominaisuudet
Kaikilla attribuuteilla on seuraavat ominaisuudet:
| Ominaisuus | Tyyppi | Kuvaus |
|---|---|---|
id |
string | Attribuutin yksilöllinen tunniste |
type |
string | Kentän tyyppi (text, textarea, select jne.) |
label |
string | Editorissa näkyvä nimike |
default |
mixed | Attribuutin oletusarvo |
help |
string | Nimikkeen vieressä työkaluvihjeenä näytettävä ohjeteksti |
position |
string | Kentän sijainti: inspector (oletus) tai toolbar |
conditions |
array | Kentän näyttämisen ja piilottamisen ehdollinen logiikka |
Sijainti
Oletusarvoisesti kaikki kentät näytetään lohkon tarkastelupaneelissa (sivupalkissa). Voit muuttaa tätä position-ominaisuudella:
JSON
{
"blockstudio": {
"attributes": [
{
"id": "alignment",
"type": "select",
"label": "Alignment",
"position": "toolbar",
"options": [
{ "value": "left", "label": "Left" },
{ "value": "center", "label": "Center" },
{ "value": "right", "label": "Right" }
]
}
]
}
}
Oletusarvot
Aseta oletusarvot default-ominaisuudella:
JSON
{
"blockstudio": {
"attributes": [
{
"id": "title",
"type": "text",
"label": "Title",
"default": "Hello World"
},
{
"id": "showBorder",
"type": "toggle",
"label": "Show Border",
"default": true
}
]
}
}
Ohjeteksti
Lisää käyttäjiä opastavaa ohjetekstiä:
JSON
{
"blockstudio": {
"attributes": [
{
"id": "apiKey",
"type": "text",
"label": "API Key",
"help": "Enter your API key from the settings page"
}
]
}
}
Attribuuttien käyttäminen
Mallissasi attribuutit ovat käytettävissä lyhenteellä $a (tai $attributes):
PHP
<h1><?php echo $a['title']; ?></h1>
<p><?php echo $a['content']; ?></p>
Tai käytä kohdetta Twig:
Twig
<h1>{{ a.title }}</h1>
<p>{{ a.content }}</p>