Mohl
Mohl

Reputation: 415

Hugo Page Resources - can't get a match

I want to write a generic shortcode for my hugo-powered webseite that adds a download section to my pages listing all the files in the downloads folder.

I have the website layed out like this:

.
├── content
│   └── press
│       ├── downloads
│       │   ├── presstext.pdf
│       │   └── presskit.zip
│       ├── _index.de.md
│       └── _index.en.md
└── layouts
    └── shortcodes
        └── downloads.html

My markdown file looks like this:

---
title: "Downloads"
date: 2019-10-26T09:59:26+01:00
draft: true

resources:
- src: downloads/presskit.zip
  title: Presskit
  params:
    icon: pdf
- src: downloads/presstext.pdf
  title: Presstext
  params:
    icon: pdf
---

Look at my awesome downloads:
{{< downloads >}}

And my shortcode looks like this:

<ul class="downloads">
    {{ range .Page.Resources.Match "downloads/*" }}
    <li>
        <a target="_blank" href="{{ .Permalink }}">
            <i class="far fa-file-{{ .Params.icon }}"></i> {{ .Title }}
        </a>
    </li>
    {{ end }}
</ul>

But no document ever get's matched, so {{ range .Resources.Match "downloads/*" }} always returns empty. Am I overlooking something?

I already tried:

{{ range .Resources.Match "downloads/*" }}
{{ range .Resources.Match "/downloads/*" }}
{{ range .Resources.Match "**.zip" }}
{{ range .Resources.Match "**.pdf" }}
{{ range .Resources.Match "press/downloads/*" }}
{{ range .Resources.Match "/press/downloads/*" }}

Running on Hugo 0.59.0

Upvotes: 2

Views: 2491

Answers (2)

IvanD
IvanD

Reputation: 2933

OK, this is an old one, but the right answer is that for branch bundles (_index.md) you can have resources ONLY in the same folder.

For leaf bundles (index.md) you can have resources in subfolders.

I guess it is because every subfolder in branch bundles is supposed to be a page with resources (leaf bundle).

Here is the source https://gohugo.io/content-management/page-bundles/ (see the table row Where can the Resources live?)

Upvotes: 3

Mohl
Mohl

Reputation: 415

I gave up on this and ended up doing this instead:

.
├── content
│   └── press
│       ├── presstext.pdf
│       ├── presskit.zip
│       ├── _index.de.md
│       └── _index.en.md
└── layouts
    └── shortcodes
        └── downloads.html

my markdown:

---
title: "Downloads"
date: 2019-10-26T09:59:26+01:00
draft: true

resources:
- src: presskit.zip
  title: Press kit
  params:
    icon: archive
    download: true

- src: presstext.pdf
  title: Press text
  params:
    icon: pdf
    download: true

---

Look at my awesome downloads:
{{< downloads >}}

Shortcode:

<ul class="downloads">
  {{ range .Page.Resources }}
    {{ if isset .Params "download" }}
    <li>
      <a target="_blank" href="{{ .Permalink }}">
        <i class="far fa-file-{{ .Params.icon }}"></i> {{ .Title }}
      </a>
    </li>
    {{ end }}
  {{ end }}
</ul>

Upvotes: 3

Related Questions