如何使用雨果查询网站数据

发布于 2025-02-05 11:11:22 字数 1071 浏览 2 评论 0原文

在雨果模板中,如何在.site.data上进行查找。假设我有这样的数据结构:

{
    "my-blog-post": {
        "Version": 21,
        "Revision": 0,
        "Code": "my-blog-post",
        "Name": "My Blog Post",
        "Parent": "the-parent-post"
    },
    "another-post": {
        "Version": 21,
        "Revision": 0,
        "Code": "another-post",
        "Name": "Another Post",
        "Parent": "the-parent-post"
    }

}

在博客文章的详细信息页面内,我想获取所有以当前帖子为父的帖子的列表。基本上,我想查询具有“父母库”作为父字段的价值的帖子。 我可以使用范围来实现这一目标,但我需要使用它们来使用它们。 我尝试了这样的表情,但没有快乐。

{{$child_posts := where (where .Site.Data "Section" "blog") "Parent" "the-parent-post" }}

我可以做到这一点:

{{$posts := where $site.Pages "Section" "blog"}}
{{ range sort $posts "Name" "asc" }}
    {{ $post := . }}
    {{if eq $post.parent $currentPage.Code}}
          ...Do something...
    {{end}}
{{end}}
    

但这不是理想的,而不是我需要的。 提前致谢。

In a hugo template how can I do a lookup on .Site.Data. Assuming I have a data structure like this:

{
    "my-blog-post": {
        "Version": 21,
        "Revision": 0,
        "Code": "my-blog-post",
        "Name": "My Blog Post",
        "Parent": "the-parent-post"
    },
    "another-post": {
        "Version": 21,
        "Revision": 0,
        "Code": "another-post",
        "Name": "Another Post",
        "Parent": "the-parent-post"
    }

}

Inside the detail page for a blog post, I would like to get the list of all the posts that have the current post as the parent. Basically I would like to query posts that have "the-parent-post" as the value of Parent field.
I can achieve this using range but I need to get them using where.
I experimented with expressions like this but no joy.

{{$child_posts := where (where .Site.Data "Section" "blog") "Parent" "the-parent-post" }}

I can do this:

{{$posts := where $site.Pages "Section" "blog"}}
{{ range sort $posts "Name" "asc" }}
    {{ $post := . }}
    {{if eq $post.parent $currentPage.Code}}
          ...Do something...
    {{end}}
{{end}}
    

But it is not ideal and not what I need.
Thanks in advance.

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

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

发布评论

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

评论(1

半衬遮猫 2025-02-12 11:11:22

您正在尝试浏览地图地图。您需要一系列地图。

https:// https://discourse.gohugo.io/ t/filtering-data-in-json-using-where/35287/2

Your are trying to range through a map of maps. You need an array of maps.

https://discourse.gohugo.io/t/filtering-data-in-json-using-where/35287/2

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