Retrieving page resource and image processing silently fail when using partials with context other than dot

Let us consider the following code with the intent of displaying the names and faces of people defined in content files. This is a page calling a partial:

<main class="container">
    {{ partial "teachers" . }}
</main>

Then, within the partial:

<section>
  <div>
    {{ range .Params.teachers }}
      {{ $imageResource := ($.Site.GetPage "section" "uploads").Resources.GetMatch (strings.TrimPrefix "/uploads/" .portrait) }}
      {{ $resizedPortrait := $imageResource.Resize "x320 q90 webp"}}
      <div>
        <img src="{{ $resizedPortrait.RelPermalink }}" alt="">
        <p>{{ .name }}</p>
      </div>
    {{end}}
  </div>
</section>

This code runs as expected. Now, it would stand to reason that we could edit the code in the following way and achieve the same result:

<main class="container">
      {{ partial "teachers" .Params }}
 </main>

Then, we change the partial:

<section>
  <div>
    {{ range .teachers }}
      {{ $imageResource := ($.Site.GetPage "section" "uploads").Resources.GetMatch (strings.TrimPrefix "/uploads/" .portrait) }}
      {{ $resizedPortrait := $imageResource.Resize "x320 q90 webp"}}
      <div>
        <img src="{{ $resizedPortrait.RelPermalink }}" alt="">
        <p>{{ .name }}</p>
      </div>
    {{end}}
  </div>
</section>

This code runs, but does not work as expected. {{ .name }} returns the correct name and {{ .portrait }} returns a correct link to the portrait, which is in the content/uploads directory. However, there is apparently no $imageResource. Even though there is no $imageResource, the image optimization script somehow runs. This is strange, as ordinarily resizing a non-existent image would cause an error. In this case, there is no error and the final result is an empty src in the img tag. I find this whole thing rather puzzling, as there is obviously an error occuring somewhere, but running hugo server produces no error message. I am currently using version 0.122.

That’s not in context.

Use the global site function instead.

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