SuperSonicSites / page_builder

0 stars 1 forks source link

Calling context within the loop #2

Open SuperSonicSites opened 3 years ago

SuperSonicSites commented 3 years ago

Hi @jimafisk

Thank you so much for taking an interest in this. I'll look into your ssg sometime today.

It's actually imperative for me to be able to call the context within the component loop. This repo is a trimmed down version of the ui kit we are building. I need to be able to call a {{ .site.params }} for our imagekit base url within an hero component for example.

After reading this https://gohugo.io/templates/introduction/#the-dot

I think the answer is something like this

{{ define "main" }}

{{ $page := . }}

{{ range .Params.Components }}

  {{ range $component, $value := . }}

    {{ $partialname := "components/%s.html" }}

    {{ partial $partialname (dict "pagecontext" $page "$value")

  {{ end }}

{{ end }}

{{ end }}

This of course. doesnt work and I'm still trying to make it work on _index.html

jimafisk commented 3 years ago

I think the challenge here is the content you're trying to pull into your partial does not actually belong to the component, it belongs to the entire content file.

From our conversation on youtube:

The way I think about components is they are "composable" in the sense that you can reuse components on the same page with different content. The problem with using {{ .Content }}, which refers to the unstructured blob of markdown at the bottom of the file is that it has a one-to-one relationship with the content file. In that sense you can't really use it to compose a template, so I would probably reach for something like the layout key in frontmatter instead (https://gohugo.io/content-management/front-matter/).