Lewati ke konten

Mendaftarkan Atribut

Atribut didefinisikan dalam array blockstudio.attributes di file block.json Anda.

Contoh Dasar

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

Properti Atribut

Semua atribut memiliki properti berikut:

Properti Tipe Deskripsi
id string Pengenal unik untuk atribut
type string Tipe field (text, textarea, select, dll.)
label string Label yang ditampilkan di editor
default mixed Nilai default untuk atribut
help string Teks bantuan yang ditampilkan sebagai tooltip di samping label
position string Lokasi field: inspector (default) atau toolbar
conditions array Logika kondisional untuk menampilkan/menyembunyikan field

Posisi

Secara default, semua field ditampilkan di pemeriksa blok (bilah samping). Anda dapat mengubahnya menggunakan properti 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" }
        ]
      }
    ]
  }
}

Nilai Default

Tetapkan nilai default menggunakan properti default:

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

Teks Bantuan

Tambahkan teks bantuan untuk memandu pengguna:

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

Mengakses Atribut

Di template Anda, atribut tersedia melalui singkatan $a (atau $attributes):

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

Atau di Twig:

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

Buku Resep Pola Field