Error within conditional even when conditional should not run

I have an error in an if that I use to check if an image url has been set. I’m getting an error when running as Hugo seems to parse the content of the if, even when it should ignore it.

{{ if and (isset .Params "imageurl") (not (eq .Params.imageurl "")) }}
  {{ $image_path := substr .Params.imageurl 0 -4 }}
  {{ $image_path }}
{{ end }}

I assume this is a nuance of the templating system, and might just be how it works, but am I missing something? Is there a better way to do this?

Does no one have anything to say on this? If more information is needed, please let me know.

The with function is similar to if, but does not fail if the param is unset. You might try that.

When you run your hugo server try it with the --verbose switch to see what’s going on. It would help if you could report the error you see there.

Thanks Rick, with have a look at that.

This is it:

Untested but something like:

{{ with .Params.imageurl }}
{{ $image_path := sub...
{{ end }}