Get parent varriable under `with` in range

why $.Resource and .Resources not same? Is it a bug or design behavior?

Hugo version : v0.136.1

{{ $related := first $params.related.count (where (where $.Site.Pages ".Params.tags" "intersect" .Params.tags) "Permalink" "!=" .Permalink) }}
{{ with $related }}
    {{ range . }}
            {{- $resource := .Resources -}}
            {{- with "aaa" -}}
                {{- fmt.Warnf "related  $.Resource is %v +====== before .Resources is %v " $.Resources  $resource -}}
            {{- end -}}
    {{ end }}
{{ end }}

output:

WARN  related  $.Resource is [featuredImage.webp] +====== before .Resources is [restarting-life-og.webp restarting-life.webp]
WARN  related  $.Resource is [featuredImage.webp] +====== before .Resources is []
WARN  related  $.Resource is [featuredImage.webp] +====== before .Resources is [thumbnail.jpg]
WARN  related  $.Resource is [featuredImage.webp] +====== before .Resources is [cloud-history.png cncf-accepted-12-new-project-2019.png crossing-the-chasm.webp gartner-2024-hype-cycle-emerging-technologies.png group-noactivities.png growth-in-china-2019.png kubecon-eu-sponsorship-status.png kubernetes-china-contributors-countries-stats.png kubernetes-comments-countries-stats.png kubernetes-commits-countries.png kubernetes-committers-countries.png kubernetes-contributor-status.jpg kubernetes-contributors-countries-stats.png kubernetes-google-trends.png kubernetes-issue-creatos-countries-stats.png nvidia-goc-2024.webp nvidia-gtc-2024.jpg practitioner-nim-1920x1080.png project-maturity-levels.webp sponsorship-history-status.jpg]

https://gohugo.io/templates/introduction/#context

You should read that page, and the “Context” section in particular, before you begin templating anything.

Make sure that you thoroughly understand the concept of context before you continue reading. The most common templating errors made by new users relate to context.

I misunderstand ‘$’ as getting the parent variable :cry:

A couple of examples…

{{ range slice "a" "b" "c" }}
  {{ . }} --> a then b then c
  {{ range slice 1 2 3 }}
    {{ . }} --> 1 then 2 then 3
  {{ end }}
{{ end }}

{{ range $foo := slice "a" "b" "c" }}
  {{ . }} --> a then b then c
  {{ range slice 1 2 3 }}
    {{ . }} --> 1 then 2 then 3
    {{ $foo }} --> a until we start the next iteration of the outer loop
    {{ $.Title }} --> returns the Title from the Page object
  {{ end }}
{{ end }}

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