Partial using (something similar to) .Resources.Match to find images in static/images

Hello!

I’m trying to add a section to the bottom of my posts to include a bunch of images. I may gussy it up later, but right now the problem I’d like to solve is that I want to be able to dump pictures into static/images with the correct filename and have them show up on the correct post. Ideally without managing lots of file structures (such as in bundles).

I don’t think it’s possible to use .Resources.Match to search the static folder, but I’m not sure what else to use? For example:

{{ $name := .File.BaseFileName }}
{{ $searchName := (printf "%s%s%s" "images/" $name "**") }}

{{ with .Resources.Match $searchName }}
    {{ range . }}
		<img src="{{ .RelPermalink }}">
	{{ end }}
{{ end }}

You can place your images in the assets directory, and then they will be available as global resources using resources.Get and resources.GetMatch.

{{ resources.Get "images/a.jpg" }} --> returns single image
{{ resources.Get "images/*.jpg" }} --> error
{{ resources.GetMatch "images/a.jpg" }} --> returns single image
{{ resources.GetMatch "images/*.jpg" }} --> returns single image (the first one to match)

Unfortunately there is not a global equivalent to .Resources.Match to obtain multiple global resources.

A headess bundle might work for you.

content
└── my-resources
    ├── images
    │   ├── a.jpg
    │   └── b.jpg
    └── index.md

content/my-resources/index.md

+++
title = "My Resources"
date = 2021-04-16T09:58:03-07:00
draft = false
headless = true
+++

template

{{ $searchName := "images/*.jpg" }}
{{ $p := site.GetPage "my-resources" }}
{{ with $p.Resources.Match $searchName }}
  {{ range . }}
  <img src="{{ .RelPermalink }}">
  {{ end }}
{{ end }}
1 Like

You should look into Hugo’s build options

This would allow you to create a Page Bundle anywhere in your content dir which would systematically (but only) publish its resources regardless of their .RelPermalink being invoked in your template. This would then act like a static directory but you could use .Resources.Match to find stuff in it.

2 Likes

This worked perfectly! Thank you for the suggestions. :slight_smile:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.