NetlifyCMS配置允许您设置专门针对文件的集合类型。在下面的设置中,目标文件被称为test.toml
. 当然,你可以让它成为目标config.toml
,但我将在这个答案的后面解释为什么你可能想要将它分开以保持它在Hugo中的清洁
如下所示,为您的收藏项指定一个file
文件夹,而不是一个文件夹。请记住,该值将是从您的存储库/项目根目录开始的路径。然后我们使用对象类型字段嵌套来配置字段以创建我们的对象路径。
config.yml
collections:
- name: "blog"
label: "Blog post"
folder: "post/"
create: true
fields:
- { label: "Title", name: "title" }
- { label: "Publish Date", name: "date", widget: "datetime" }
- { label: "Description", name: "description", widget: "markdown", required: false }
- { label: "Featured Image", name: "image", widget: "image", required: false }
- { label: "Body", name: "body", widget: "markdown", required: false }
- label: "Config"
name: "configs"
files:
- name: "test"
label: "test.toml"
file: "test.toml"
fields:
- name: "params"
label: "Params"
widget: "object"
fields:
- name: "tiles"
label: "Tiles"
widget: "object"
fields:
- {label: "Enable", name: "enable", widget: "boolean"}
- name: "showcase"
label: "Showcase Items"
widget: "list"
create: true # Allow users to create new documents in this collection
fields:
- { label: "Title", name: "title", widget: "string" }
- { label: "Sub Title", name: "subtitle", widget: "string" }
- { label: "Image", name: "image", widget: "image", required: false }
- { label: "URL", name: "url", widget: "string" }
输出:
test.toml
[params]
[params.tiles]
enable = true
[[params.tiles.showcase]]
image = "/images/some-image.jpg"
subtitle = "SubTitle 1"
title = "Title 1"
url = "/path1/"
[[params.tiles.showcase]]
image = "/images/some-other-image.jpg"
subtitle = "SubTitle 2"
title = "Title 2"
url = "/path2/"
建议 (Hugo):由于此配置将针对某个展示页面的集合,因此最好将其放入带有前端 ( tiles/_index.md
) 的文件位置或创建数据文件 ( data/tiles.json
)。config.toml 中的所有字段都需要配置为能够写出集合。此时,您不能在 NetlifyCMS 中的文件设置中仅指定文件的一部分作为目标。