雨果数据模板:在哪里创建文件以及将其放入什么?

发布于 2025-01-30 13:34:58 字数 516 浏览 2 评论 0原文

我有一个雨果SSG网站。

我的json文件保存在:/data/source.json

我创建一个模板文件:/layouts/test.html具有此内容的内容:

<!-- Data is in /data/source.json -->
{{ range .Site.Data.source }}
{{ range .names }}
<!-- person_name -->
<p>{{ .identifier.person_name }}</p>
{{ end }}
{{ end }}

我该如何渲染。 HTML内容到网站?

雨果文档在这里制作.html文件后我没有解释我需要做什么。

I have a Hugo SSG website.

My JSON file is saved in: /data/source.json

I created a template file: /layouts/test.html that has this content:

<!-- Data is in /data/source.json -->
{{ range .Site.Data.source }}
{{ range .names }}
<!-- person_name -->
<p>{{ .identifier.person_name }}</p>
{{ end }}
{{ end }}

How do I render this .html content to the website?

The Hugo documentation here doesn't explain what I need to do after making the .html file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香草可樂 2025-02-06 13:34:58

看起来您只需要一个范围操作。您没有在多个来源上范围。JSON文件,对吗?

假设您有一个文件data/source.json这样:

{
  "names": [
    {
      "identifier": {
        "person_name": "Joe Wilson"
      }
    },
    {
      "identifier": {
        "person_name": "Nancy Wilson"
      }
    }
  ]
}

那么应该可以渲染这样的名称:

  {{ range .Site.Data.source.names }}
    <p>{{ .identifier.person_name }}</p>
  {{ end }}

将其添加到themes/{theme {themem {themem {theme布局将使用该模板在页面上渲染。

It looks like you have two range operations when you need only one. You are not ranging over multiple source.json files, correct?

Assuming you have a file data/source.json like this:

{
  "names": [
    {
      "identifier": {
        "person_name": "Joe Wilson"
      }
    },
    {
      "identifier": {
        "person_name": "Nancy Wilson"
      }
    }
  ]
}

Then it should be possible to render those names like this:

  {{ range .Site.Data.source.names }}
    <p>{{ .identifier.person_name }}</p>
  {{ end }}

Add that to a layout in your themes/{theme-name}/layouts and it will render on the pages using that template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文