doouzlrvb01417498 2017-04-16 18:37
浏览 113
已采纳

如何通过Markdown名称遍历Hugo页面

I am building a simple Hugo blog and I have this following toml config for a page

+++
[publications]
links = ["2017/article1",
        "2017/article2"]
+++

And I have these files in their appropriate content section (content/publications/2017/article1.md). What I need is to iterate through them, load each page and use some of their .Params in building a partial. Something like

 {{ range .Params.publications.links }}
 {{ do something with page parameters }}
 {{ end }}

I guess it is a basic Hugo question, I just cant figure it out.

  • 写回答

1条回答 默认 最新

  • dsx666666 2017-04-17 05:53
    关注

    This actually requires some pretty advanced use of Hugo templates. But you can do it!

    First, to make it easy for yourself, add the ".md" extension to the pages you are trying to access. It's probably also a good idea to add the full path so that Hugo doesn't get the wrong file if you add files with the same name in a different directory in the future.

    +++
    [publications]
    links = ["publications/2017/article1.md",
            "publications/2017/article2.md"]
    +++
    

    Then you can use something like the following in your template.

    {{ range .Params.publications.links }}
      {{ range where $.Site.Pages "URL" ($.RelRef .) }}
        The "{{ .Title }}" page has {{ .WordCount }} words.
      {{ end }}
    {{ end }}
    

    This uses the where function to filter the array of all the site's pages by the URL field. To find the URL it uses the .RelRef page variable with the link text.

    I think there should also be a way to use the apply function to do this without the inner range statement, but I couldn't get it to work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?