Image processing from partial not working

I’m deplying to Netlify, so I need to store all images that will be CMS-editable in a directory in /content, as documented in this solution. It works well in my layout files, but when I try to drop it into a partial it won’t work. I can print out $img below line 1 in the example below, but the <img> tag is never generated. I’m passing in the context when calling the partial: {{ partial "person.html" . }}. No errors when running the server or plain build script, nothing shows up with the --verbose option. Not sure what else I should be looking at to troubleshoot…

{{ $img := strings.TrimPrefix "/uploads/" .image }}
{{ $name := .name }}
{{ with $.Site.GetPage "page" "uploads/" }}
    {{ with .Resources.GetMatch $img }}
        {{$imgFill := .Fill "124x124 Center"}}
        <img class="" src="{{ $imgFill.RelPermalink }}" width="124" height="124" alt="Photo of {{ $name }}" tabindex="0">
    {{ end }}
{{ end }}

Is this the code from your partial?

This code uses a map context with .image and .name and yet still tries to find the .Site variable in its root context ($).

Because you’re cautiously using with no error is produced, but unless you passed .Site to your partial map context as “Site”. There’s no chance for this with clause to pass successfully.

Now since Hugo 0.53, .Site is globally accessible through the namespace site. So you should be able to do what you need by replacing line 3 with

{{ with site.GetPage "page" "uploads/" }}

:point_up: Also, you don’t need to content type in the .GetPage calls anymore

1 Like