If and context issues inside a shortcode - ambigous URL context

Using .Scratch I got this to work

    {{ with $.Site.Params.imagespath }} {{ $.Scratch.Set "path" ( . ) }} {{ else }}{{ $.Scratch.Set "path" "/images/" }}{{ end }}
    <div class="box box--column box--image">
    <img src="{{ $.Scratch.Get "path" }}{{ .Get "filename" }}" />
    </div>

local variable is cleaner

{{ $path := "/images/" }}
{{ with $.Site.Params.imagespath }} {{ $path := ( . ) }}{{ end }}
<div class="box box--column box--image">
<img src="{{ $path }}{{ .Get "filename" }}" />
</div>

But this is what I tried originally that I could not get to render. Something about context maybe within shortcodes?

<div class="box box--column box--image">
<img src="{{ if $.Site.Params.imagespath }}{{ $.Site.Params.imagespath }}{{ else }}/images/{{ end }}{{ .Get "filename" }}" />
</div>
 ERR: html/template:theme/shortcodes/image.html:2:104: {{.Get "filename"}} appears in an ambiguou
s URL context

just to be sure I saved the shortcode parameter locallly but I still get the same error

{{ $file := .Get "filename" }}
<div class="box box--column box--image">
<img src="{{ with $.Site.Params.imagespath }}{{ . }}{{ else }}/images/{{ end }}{{ $file }}" />
</div>
 ERR: html/template:theme/shortcodes/image.html:2:104: {{ $file }} appears in an ambiguou
s URL context

How come . context is lost here. The if statement doesn’t create any new context and I’m grabbing imagespath with $.

Beside the “why is this happening” is there an alternative to what I found that is “slicker” and more terse?