Hoppa till innehåll

Registrera attribut

Attribut definieras i arrayen blockstudio.attributes i din fil block.json.

Grundläggande exempel

JSON
{
  "name": "my-theme/my-block",
  "title": "My Block",
  "blockstudio": {
    "attributes": [
      {
        "id": "title",
        "type": "text",
        "label": "Title"
      },
      {
        "id": "content",
        "type": "textarea",
        "label": "Content"
      }
    ]
  }
}

Attributegenskaper

Alla attribut har följande egenskaper:

Egenskap Typ Beskrivning
id string Unik identifierare för attributet
type string Fälttypen (text, textarea, select osv.)
label string Etikett som visas i redigeraren
default mixed Standardvärde för attributet
help string Hjälptext som visas som ett verktygstips bredvid etiketten
position string Var fältet visas: inspector (standard) eller toolbar
conditions array Villkorslogik för att visa eller dölja fältet

Position

Som standard visas alla fält i blockinspektören (sidofältet). Du kan ändra detta med egenskapen 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" }
        ]
      }
    ]
  }
}

Standardvärden

Ange standardvärden med egenskapen default:

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "id": "title",
        "type": "text",
        "label": "Title",
        "default": "Hello World"
      },
      {
        "id": "showBorder",
        "type": "toggle",
        "label": "Show Border",
        "default": true
      }
    ]
  }
}

Hjälptext

Lägg till hjälptext som vägleder användare:

JSON
{
  "blockstudio": {
    "attributes": [
      {
        "id": "apiKey",
        "type": "text",
        "label": "API Key",
        "help": "Enter your API key from the settings page"
      }
    ]
  }
}

Åtkomst till attribut

I din mall är attribut tillgängliga via kortformen $a (eller $attributes):

PHP
<h1><?php echo $a['title']; ?></h1>
<p><?php echo $a['content']; ?></p>

Eller i Twig:

Twig
<h1>{{ a.title }}</h1>
<p>{{ a.content }}</p>

Kokbok med fältmönster