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

如何通过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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!