Help with passing dict to partial with resources

I’ve been tinkering on this for the past hour, but can’t get it to work.

I’m trying to pass a variable, $pages, to a partial with dict.

In layouts/product/list.html, I have:

{{$pages := where .Pages "Layout" "==" ""}}
{{partial "products/list.html" (dict "products" $pages)}}

Then in partials/products/list.html, I have:

{{range .products}}
{{$images := ($.Site.GetPage "/products/images").Resources}}
{{$img := $images.GetMatch (printf "%s.*" (anchorize .Title))}}
<img src="{{with $img}} {{.RelPermalink}} {{end}}"/>
{{end}}

This will not work. But doing the following does:

In layouts/product/list.html:

{{partial "products/list.html" .}}

Then in partials/products/list.html:

{{$pages := where .Pages "Layout" "==" ""}}
{{range $pages}}
{{$images := ($.Site.GetPage "/products/images").Resources}}
{{$img := $images.GetMatch (printf "%s.*" (anchorize .Title))}}
<img src="{{with $img}} {{.RelPermalink}} {{end}}"/>
{{end}}

What am I missing?

Pass the context to your partial as well. Then inside your partial, instead of $.Site you would write .ctx.Site

{{ partial "products/list.html" (dict "products" $pages "ctx" $) }}
2 Likes

Thanks a bunch.

Your suggestion worked with one small modification. I had to add $ to .ctx.Site so $.ctx.Site.