Templates with optional content zones

I’m building a site, which has multiple companies. Most companies can share the exact same template, but some need an extra region on the page, where each company can have their own box, for example with some advertising, or recruitment messages.

I’ve created a small demo at https://github.com/paulclarkin/hugo-help

I’ve read all the documentation about blocks and regions, and partials but I can’t figure out how to make this work.

I’d like the company template to look a bit like this:

{{ define "main" }}
{{ .Content }}
  {{ IF Optional Zone }}
    {{ RENDER Optional Zone HTML }}
  {{ end }}
{{ end }} 

and then be able to supply the HTML for the Optional Zone from somewhere in the content folder.

Am I trying to do something impossible or have I missed something obvious?



You could do something like:

{{ define "main" }}
  {{ .Content }}

  {{ if condition }}
    {{ partial "foo.html" . }}
  {{ end }}

{{ end }} 

I’m not entirely sure what you mean by this, but you would then put the code to do this into the appropriate partial partials/foo.html

Will foo.html be able to have its own {{. Content}} ?

I think what I should do is add the “default” content to the company’s section front matter through companies/_index.md.

Then on any given company single template you can use:

<h3>Default Content</h3>
{{ .Parent.Content }}
{{ with .Content }}
<h3> Company's own Content</h3>
    {{ . }}
{{ end }}

It should work with your current dir structure even though all you have is the home and then one company. My example work for several companies living under a shared directory.

Thanks for the suggestions. In the end I went with a hybrid solution that works for now.

{{ if  ($.Param "company.subpanel") }}
    {{ partial (printf "%s-subpanel.html" ($.Param "company.name") ) . }}
{{ end }}

Each sub panel will just be a partial, but it works for now.

1 Like