从 Netlify CMS 添加到 Hugo 站点变量

Add to Hugo site variables from Netlify CMS

我正在使用 Forty Theme for Hugo with Netlify CMSconfig.toml 文件有一个 Tiles 部分,如下所示:

# Tiles Section
[params.tiles]
enable = true

  # Display your showcases here.
  [[params.tiles.showcase]]
  title = 
  subtitle = 
  image = 
  url = 

我希望能够从 CMS 向磁贴部分添加条目。如何从 CMS 向 params.tiles.showcase 添加变量?这是我 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 }

NetlifyCMS 配置允许您设置 collection 类型专门针对文件。在下面的设置中,目标文件称为 test.toml。当然,您可以将其设为 config.toml 目标,但我稍后会在本回答中解释为什么您可能希望将其分开以保持其在 Hugo

中的清洁

为您的 collection 项目指定 file 而不是文件夹,如下所示。请记住,该值将是从您的 repository/project 根目录开始的路径。然后我们使用 object 类型字段嵌套来配置字段以创建我们的 object 路径。

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): 由于此配置将针对某些展示页面的 collection,因此最好将其放入文件位置front matter (tiles/_index.md) 或创建数据文件 (data/tiles.json)。 config.toml 中的所有 字段需要配置才能写出 collection。此时您不能在 NetlifyCMS 中的文件设置中仅指定要定位的文件部分。