Using .Resources within partial without misusing $.Scratch

My current workaround to use .Resources.Match within a partial and getting more specific data for this specific part of the page.

The content structure using page bundles:

content/index/index.md <- page data
content/index/some-partial.md <- partial data
content/index/some-partial/more-data.md <- more specific partial data, which needs to be loopable

Current workaround:

single.html:
{{ range sort .Resources }}
  {{ $.Scratch.Set "data" . }}
  {{ partial "fragments/test.html" $ }}
{{ end }}

fragments/test.html:
{{ $self := ($.Scratch.Get "data").Params }}
{{ $name := ($.Scratch.Get "data").Name }}
{{ $name_t := strings.TrimSuffix ".md" $name }}
{{ $content := .Resources.Match (printf "%s/*" $name_t) }}
{{ range $content }}
  {{ .Params }}
{{ end }}

A few problems with the above:

  • passes the whole site data to each partial
  • uses scratch
  • workaround to get current file name

Why not just pass the dot context into the partial. So:

{{ partial "fragments/test.html" . }}

Then you can get rid of scratch set/get completely.

Also I believe range does the sorting by default… so just range instead of range sort would work the same.

Thanks for the range sort tip. Was some leftover code from another workaround.

Unfortunately using the context “.” doesn’t work as then one can’t access .Resources.Match within the partial. This is essential to access the sub page bundles.

{{ partial "fragments/test.html" . }}

In that case, you can “pass multiple contexts” to the partial… as a slice. Pass the whole .Resources as well as the current slice element of .Resources as a 2-element slice to the Partial.

1 Like

If you need to pass “more than one” data object to a partial, see

1 Like

Thanks. That would simplify the code and reduce the data moved into the partial

single.html:
{{ range .Resources }}
  {{ partial "fragments/test.html" (dict "resources" .Resources "self" .) }}
{{ end }}

fragments/test.html:
{{ $self := .self.Params }}
{{ $name := .self.Name }}
{{ $name_t := strings.TrimSuffix ".md" $name }}
{{ $content := .Resources.Match (printf "%s/*" $name_t) }}
{{ range $content }}
  {{ .Params }}
{{ end }}

Leaves the name usage, which still seems hacky and weird.

I’d be surprised if that would work. I think you will need to assign the whole .Resources to a var outside the range, and then pass that var as value to the “resources” dict key.

He could probably do

{{ partial “fragments/test.html” (dict “resources” $.Resources “self” .) }}

Note that i have not studied what he is trying to do here.

1 Like

Yeah you are correct. It worked before as I passed the whole $ Site data into the partial therefore .Resources was easily accessible. :frowning:

Well, same here. I am just eyeing the syntax.

Perfect. Missed that.

For context.

So the idea is to have a content structure such as:

content/index/index.md <- main page data
content/index/team.md <- team partial
content/index/team/gopher-1.md <- team member specific data
content/index/team/gopher-2.md
content/index/team/tiny-gopher.jpg <- media name specified in gopher-1.md