Passing image variable to partial

This code passes class and alt variables, but no image. How to fix it?

            {{- with resources.Get "img/imagemain.webp" -}}

              {{ $ctx := dict

                "src" .

                "class" "parallax-img" 

                "alt" "Main title"

              }}

              {{- partial "imgoptim_static" $ctx -}}

              {{ end -}}

and partial has this:

{{ $src := . }}

{{ $class := .class }}

{{ $alt := .alt }}

{{- with $src }}

<img srcset='

{{- if ge .Width "480" -}}

  {{ (.Resize "480x").RelPermalink }} 480w,

{{- end }}

{{- if ge .Width "800" -}}

  {{ (.Resize "800x").RelPermalink }} 800w,

{{- end }}

{{- if ge .Width "1200" -}}

  {{ (.Resize "1200x").RelPermalink }} 1200w{{- end }}' sizes="(max-width:600px) 480px, (max-width:1024px) 800px, 1200px" src="{{ .RelPermalink }}" class="{{ .class }}" alt="{{ .alt }}">

{{- end }}

you have a context problem:

the first should be {{ $src := .src }}

and in your partial within the with $src block use the variables not the dot-notation to refer to the values of class and alt.

before with the context is your passed dict.
inside with the context is your passed src

2 Likes

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