跳转到正文

组件

组件是 Blockstudio 区块,会经过完整的处理流程(属性、模板、资源、RPC、Tailwind),但不会出现在 WordPress 编辑器的插入器中。它们旨在构建可复用的 UI 部件,仅通过编程方式渲染。

block.json

在 blockstudio 对象中将 component 属性设为 true:

JSON
{
  "$schema": "https://blockstudio.dev/schema/block",
  "name": "my-theme/card",
  "title": "Card",
  "blockstudio": {
    "component": true,
    "attributes": [
      {
        "id": "title",
        "type": "text",
        "label": "Title"
      },
      {
        "id": "description",
        "type": "textarea",
        "label": "Description"
      }
    ]
  }
}

渲染

组件可以通过两种方式渲染:

字符串渲染器

在页面模板或文章内容中使用 <bs:> 标签:

HTML
<bs:my-theme-card title="My Card" description="Card content." />

PHP

在 PHP 模板中使用 bs_render_block() 或 bs_block():

PHP
bs_render_block([
  'name' => 'my-theme/card',
  'attributes' => [
    'title' => 'My Card',
    'description' => 'Card content.',
  ],
]);

支持的功能

组件支持所有 Blockstudio 功能:

  • 属性和字段类型
  • PHP、Twig 和 Blade 模板
  • CSS/SCSS 资源(在组件渲染时加载)
  • Tailwind CSS
  • 服务器函数(RPC)
  • 作用域样式

不适用的功能

由于组件不会出现在编辑器中:

  • 不提供区块编辑器预览
  • 区块插入器搜索无法找到这些组件
  • 组件不会调用 registerBlockType()