Optionally add stuff to a partial

So, my template has a partial header.html which is mainly the html head section with some stuff added to it. I also want to be able to add styles and javascript there that matters only on certain layouts.

So I put a block in there

layout stuff
{{ block "header" . }}{{ end }}

However, now, when I write to this block from different layouts, the last layout to do so sticks and this insertion will appear on all pages (because every page uses this partial). I kind of understand why this is happening, but could not come up with a solution for my problem.

What can I do?

Just pass an other context to the partial.

Example you can adapt with your section or layout or whatever.

{{ partial "image_asset.html" (dict "context" . "path" .image "size" "x400" "action" "Resize" ) }}

And manage the parameters inside the partial

{{- $size := .size -}}
{{- $path := .path -}}
{{- $action := .action | default "Fill" -}}
{{- $filter := .filter | default "" -}}

Wouldn’t I have to somehow redefine

. := .context

if . has a “context” in the partial for it to work?

I tried to pass in the dict and it broke the layout (something rendered <nil> in the head section). However, it still kept the header block from another layout. From what I read, this should not happen.

To be clear:

  • all pages use the header partial
  • in the header partial is a block
  • several pages define this block
  • one of these pages wins and overwrites the other definitions (the last to be parsed?)
  • now all pages got this block definition

Base Templates and Blocks gives me the impressions these block definition should not cross the template’s border.

Hi there,

Please have a read about Requesting Help . It is easier to help if we can see your site code instead of us trying to replicate your setup.

Inside your partial, now the context is no more variable ., but now variable context.
Adapt the partial acordingly.

And use $ to access something outside context (ex : $.Site.Params.myvariable)

Find great @martignoni article on context and the dot.

It is easier to help if we can
see your site code instead of us trying to replicate your setup.

Sure, you are right. The code is so trivial, that I suspected more of an
overall issue than something that can be pinpointed, but even under
circumstances, I concur.

I put the thing on github, for the whole world to see my shame :slight_smile:
https://www.temporary-url.com/2E0158 (this is a github link that will
in 7 days to somewhat protect my privacy).

The problem arises with the files in /layouts/portfolio and /partials/

You are not using baseof templates (for example, layouts/_default/baseof.html).

You are not using baseof templates (for example,

Exactly, but my theme does not use them neither. Does this mean, I should find
another theme to start with, one that makes use of this pattern? I checked a
few, none did. I assume, if I have to override a theme’s layout’s base, little
of the original theme will be left.

Does this mean, I can use these blocks only, when my template has this base?

As an alternative, I could include another, empty partial in head, that I
could override when the need arises, right? I have seen this in the themes, I

While the documentation gives a lot of clues it rarely if ever gives a
complete picture. These things are difficult to figure out on your own…

You need a base template (baseof) in which to define the blocks, which you can then override on other templates.

The Quickstart uses the Ananke theme, which has a baseof defined.

baseof is a reserved name or just a convention?
I would like to keep the progress I already made with my non-baseof theme. Which of course starts with an index.html page.

Or - do I have to use a theme at all? I am starting to think it would be way easier to just code my own layout in /layouts and forget about these themes at all.

baseof is a reserved name and it has a special meaning in Hugo.

You don’t need to use a theme.