如何使用雨果查询网站数据
在雨果模板中,如何在.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试浏览地图地图。您需要一系列地图。
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