HUGO

Get the 1st value of range

I am ranging a few pictures in a lightbox on page single.html:

imgs:
  - image: "img/personal/f7-1.webp"
    imalt: "img/personal/f7-1.jpg"
  - image: "img/personal/f7-2.webp"
    imalt: "img/personal/f7-2.jpg"

But now I want to get the 1st image of that range as the meta Open Graph image:

{{ with .Params.imgs }}
  <meta property="og:image" content="{{ . | absURL }}">
  {{ end }}

- it produces no error, but picks no value.
I can range all of them, but I need only the 1st one.
I tried the first function, but can’t make out how it works.
Can someone tell me how to get the 1st image?
Thank you!

Hello,

You should use index

{{ with index .Params.imgs 0 }}

Unfortunately {{ with index .Params.imgs 0 }} throws an error message:

executing "partials/head.html" at <index .Params.imgs 0>: error calling index: index of untyped nil

I’ve found the solution (thanks to moorereason), if someone is interested:

  {{ if isset .Params "imgs" }}
  {{ range first 1 .Params.imgs }}
  <meta property="og:image" content="{{ .image | absURL }}">
  {{ end }}
  {{ end }}

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