I am looking here for advice from the most devoted people since I am finding this thing with partials a structural issue.
To give a heads-up, I am building a presentation site for a company using Bootstrap. Layouts are highly different, and I approach partials as flexible building blocks (i.e., a container with image background, heading and buttons) rather than themed content building blocks (i.e., a partial is used exclusively as a container to inform about “spaceship manufacturing” with a different spaceship name at multiple sites").
Now the problem is the way I define and use the content in the partials. I want the partials to be content-agnostic and assume only predefined keys. Look at this example:
Content spaceships.md
:
---
title: Spaceships
layout: spaceships
...
spaceships_building:
heading: Building spaceships
text: We build spaceships for anyone anywhere!
---
Layout spaceships.html
:
{{ define "main" . }}
...
{{ partial "infobox.html" (dict "context" . "data" .Params.spaceships_building) }}
...
{{ end }}
Partial infobox.html
:
<!-- I still have the access to the page context -->
{{ with .data }}
<div class="row">
<div class="col-12">
<h1>{{ .heading }}</h1>
<p>{{ .text }}</p>
</div>
</div>
{{ end }}
I feel the bad practice here is that I pass the same data twice, once with the page context, the second time as the variable .data
itself. But at least, the partials can only assume the given names, like .data
and than of course, the unified structure within these data (.heading
, .text
).
I see two alternatives to this approach, which both I feel are even worse for my case.
One, I can scope the infobox.html
partial directly in the spaceships.html
layout. But that will prevent me from accessing the page content whatsoever.
The other, I can just call the partial as usually: {{ partial "infobox.html" . }}
and scope by the desired front-matter key in the partial:
<!-- infobox.html -->
{{ with .Params.spaceships_building }}
<div class="row">
<div class="col-12">
<h1>{{ .heading }}</h1>
<p>{{ .text }}</p>
</div>
</div>
{{ end }}
But now the partial assumes the “spaceships_building” section and is practically non-reusable in my setting.
I am definitely missing something cunning here and thus, searching for an alternative to these approaches so I can use partials in this way without negative side effects. Thanks!