主题支持从种子脚本导入示例内容,包括数据库内容和 Tailor 蓝图。主题内部一个名为 seeds 的特定文件夹与目录结构一起用于提供内容。
下方您可以看到一个示例种子目录结构。blueprints 目录包含主题使用的任何蓝图模板,这些会自动导入到 app/blueprints 目录并带有一个名为 mywebsite 的嵌套目录。data.yaml 文件包含关于如何将内容导入数据库的说明。
├── themes
| └── mywebsite
| └── seeds ← 主题种子目录
| ├── blueprints
| | └── post.yaml ← 蓝图文件
| ├── lang
| | └── en.json ← 语言文件
| ├── data
| | └── blog-posts.json ← 数据文件
| └── data.yaml ← 填充脚本
由于蓝图不依赖于任何特定的文件或目录结构,它们可以自由移动。
导入蓝图时,只需将蓝图文件放置在 blueprints 目录中。它不使用任何配置,在初始化时所有蓝图都只是被复制到 app/blueprints 目录。内部会创建一个新目录,该目录与主题同名。蓝图被放置在这个新目录中。
作为一项可选功能,语言可以导入到 app/lang 目录,方法是将 JSON 语言文件 放置在 lang 目录中。这使得翻译蓝图中的标签和其他描述成为可能。如果应用程序语言目录中已存在语言文件,那么语言字符串将被合并。
该 data.yaml 文件包含一种用于将内容导入到数据库的特定格式。在下面的示例中,两组数据被导入到数据库,用于 Tailor 条目内容。
-
name: Blog Post Data
class: Tailor\Models\RecordImport
file: seeds/data/blog-posts.json
attributes:
file_format: json
blueprint_uuid: edcd102e-0525-4e4d-b07e-633ae6c18db6
-
name: Blog Category Data
class: Tailor\Models\RecordImport
file: seeds/data/blog-categories.json
attributes:
file_format: json
blueprint_uuid: b022a74b-15e6-4c6b-9eb9-17efc5103543YAML 文件应该定义一个数组,其中数组中的每个项都支持以下属性。
| Property | Description |
|---|---|
| name | gives the import step a name to display to the user. |
| class | refers to a model that extends the interface of Backend\Models\ImportModel. |
| file | refers to the JSON data file that contains the content to import. |
| attributes | a list of attributes to set on the Import Model before importing. |
以下是一个可用于导入博客分类的 JSON 文件示例。JSON 数组中的每个项目都会在数据库中生成一个具有所提供属性的导入记录。提供一个 id 属性允许记录在多次导入之间进行关联。
[
{
"id": 1,
"title": "Announcements",
"slug": "announcements"
},
{
"id": 2,
"title": "News",
"slug": "news"
}
]该 theme:seed Artisan 命令用于填充一个主题。
php artisan theme:seed <theme name>你也可以使用 --root 选项来指示命令将蓝图导入到根目录,而不是嵌套目录中。
php artisan theme:seed <theme name> --root:::tip
您也可以通过管理员面板,导航至 设置 → 前端主题 → 管理 → 初始化内容 来初始化主题。
:::