从 Netlify CMS 添加到 Hugo 站点变量

Tho*_*mas 4 hugo netlify-cms

我正在使用Hugo 的Forty Theme和Netlify CMS,该config.toml文件有一个 Tiles 部分,如下所示:

# Tiles Section
[params.tiles]
enable = true

  # Display your showcases here.
  [[params.tiles.showcase]]
  title = 
  subtitle = 
  image = 
  url = 
Run Code Online (Sandbox Code Playgroud)

我希望能够将条目从 CMS 添加到图块部分。如何params.tiles.showcase从 CMS 添加变量?这是我的一个片段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 }
Run Code Online (Sandbox Code Playgroud)

tal*_*ves 5

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" }
Run Code Online (Sandbox Code Playgroud)

输出:

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/"
Run Code Online (Sandbox Code Playgroud)

建议 (Hugo):由于此配置将针对某个展示页面的集合,因此最好将其放入带有前文 ( tiles/_index.md) 的文件位置或创建一个数据文件 ( data/tiles.json)。config.toml 中的所有字段都需要配置为能够写出集合。此时,您无法在 NetlifyCMS 中的文件设置中仅指定要定位的文件的一部分。