Trying to get the first element of a list

{{ $embedded := $.Page.Resources.ByType "image" }}
{{ $img := $.Page.Param "image" | first 1 $embedded }}
{{ with $img }}
{{- $thumbHref := .Fill $thumbCrop }}
<img src="{{$thumbHref.RelPermalink}}" />
{{ end }}

this throws:

execute of template failed: template: _default/list.html:13:46: executing “main” at : wrong number of args for first: want 2 got 3

Strangely, I cannot see this third argument at all.

However, I’m unsure if first would give me what I want at all, because it just returns another array. I’d probably would have to range through that, but isn’t there some way to get $embedded[0] or something along the lines?

I’m not sure I understand what you are trying to accomplish here, but here is why it’s not working and you are getting that error: Templating | Hugo

You are using a pipe “|” . The result of the statement before the pipe is passed on as an argument to the next statement after the pipe. So to translate, what you are actually doing is:

{{ $img := first 1 $embedded ($.Page.Param "image") }}
                 1st   2nd arg        3rd arg

and as you can see, there are in fact three arguments.

There is the index function.

1 Like

I was trying to do an or :slight_smile:
There should have been a default in the middle …

But your answer helped me a lot, thanks!