0

语境

我正在寻找一种循环遍历json 数组文件以填充 Hugo 项目中的视频库的好方法。这是我的想法的一般想法:

content/
    some_section/
        index.md  (uses {{<iframe_gallery>}})
        videos.json
    ...

视频.json

[
    {
        "title": "some video",
        "url": "https://www.youtube.com/embed/kjkdskdck"
    },
    {
        "title": "some other video",
        "url": "https://www.youtube.com/embed/kjzflzjan"
    }
]

我发现了很多关于如何在go中加载和解析 json 文件的有价值的资源,比如这个

问题

去去去模板对我来说是很新的。我不明白如何将我读到的关于实现这一点的内容转换为 go-templates。我天真地开始了

iframe_gallery.html

<section class="flex-ns flex-wrap justify-around center">
    {{ $jsonFile, $err := os.Open("./videos.json") }}

并得到

Process: loading templates: "/home/zar3bski/Documents/Code/Sites/melainya/layouts/shortcodes/iframe_gallery.html:2:1": parse failed: template: shortcodes/iframe_gallery.html:2: too many declarations in command

我觉得我错过了一个重要的区别,但未能以综合的方式理解它。你将如何加载、解析和循环这个 json 数组?有没有更直接的方法来实现这个目标?

4

0 回答 0