Substr works on Page Variable but not on array index!

I’m really puzzled here. This code works fine

<img src="{{ .Site.Params.url }}f_auto,q_70,w_736/{{ substr .Params.image 52 }}">

But when I index an array, I get error calling substr: start position out of bounds for 0-byte string:

<img src="{{ .Site.Params.url }}f_auto,q_70,w_736/{{ substr (index .Params.images 0) 52 }}">

Any clues as to what’s going on? Thanks!

Are you sure that this result is not nil or empty string?

index .Params.images 0
1 Like

Yep, it’s returning a string if I try it without the substr function, and it’s wrapped in an {{ if .Params.images }} statement as well. What’s a bit peculiar is that if I comment out the substr, start the Hugo server in fast render mode, then uncomment, it does work!

FWIW, I’ve also tried creating a variable {{ $featureimage := (index .Params.images 0) }} and using substr on that, but it doesn’t work either.

Even stranger is that substr does work if 2 parameters are given! I’ve had a trawl through the functions, I found that this works instead:

{{ strings.TrimLeft .Site.Params.url (index .Params.images 0) }}

Still curious as to why substr not working with one parameter (plus it’s less verbose)… :thinking: