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.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度