从Netlify CMS添加到Hugo站点变量

时间:2018-01-21 10:04:04

标签: hugo netlify-cms

我使用Forty Theme为Hugo 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 }

1 个答案:

答案 0 :(得分:2)

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的文件设置中指定要定位的文件的一部分。