How to get image defined in frontmatter

+++
[params]
  images = ['images/post-7/cover.jpg']
+++

I am not getting any image with this code

{{ with .Params.images }}
  {{ with index . 0 }}
    {{ with resources.Get . }}
      <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
    {{ else }}
      {{ errorf "Unable to find image %s" . }}
    {{ end }}
  {{ end }}
{{ end }}

I need it as a fallback for an image variable I have.

which with does not deliver a result? add some errorf statements in an else to see

if your error is printed - where is your image located assets/images...? if i’s in the page bundle try $.Resources.Get

what’s the sourrounding code? sure to have the right context?

lot of guesses with just that snippets…

for completeness: capitalization on *nix based systems.

I tested on a blank hugo site hugo new theme quicksite --themesDir .

quite brief answer for the possible causes listed above. another point added to the ones above: maybe a corrupt image

i plugged in your code in a “blank hugo site” (150.0) and it works - as expected.

Trying to replicate yours to get it fail like it fails for you without knowing anything about the place where it fails, what you have and any messages/debug … is waste of time

what about sharing that complete small repo?

1 Like

You are correct that it works when calling the image directly. I suspect I have a context issue. Here’s the repo GitHub - tikenak-20/quicktest: Hugo quick test site

Files of interest.

  1. home.html
  2. partials/thumbnails.html and partials/featured-image.html
  3. assets/images/post-1/cover.jpg
  4. content/posts/post-1.md

The image does show up in other list pages, except the homepage.

Edit: And the was issue contextual (the dot.) I had to pass the page context to the home image partial as a dictionary.

in featured image partial {{- $images := partial "thumbnails.html" .Page }}
in home.html {{- partial "list/featured-image.html" (dict "Page" . "Resources" .Resources "LazyLoading" $lazyload ) }}

yepp. whatever you tried with passing .Resources wont work. just pass the page

if i were you I would use a page resource for that instead of a global resource - your image seems to be page dependent

1 Like

I made an edit to previous comment.

I am actually recreating my theme for multi-use purposes and capturing all possible scenarios where images may exist. Hence why I am doing the test proofing.

glad it works now. so pointing to context wasn’t that bad. we all have been there :wink:

It was the most frustrating thing for me when I first used Hugo.

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