Having trouble setting up multiple blocks per piece of content

My site has a two column layout. In my main baseof.html template, I have two blocks, main for the primary column and secondary for the secondary column. As expected, the markdown of my posts in rendered in the primary column. However, in the same content file I’d like to also define the content of the secondary block.

I looked in the documentation and couldn’t find a way to do this out of the box (though, I could have missed something), so I thought I could do something with short codes. I set up the following secondary short code:

{{ define "secondary" }}
	{{ .Inner }}
{{ end }}

And then in my content, I want to use it like this:

{{< secondary >}}
This should hopefully be in the secondary column.
{{< /secondary >}}

However, I’m having trouble with context here, since it gives errors and it doesn’t seem I can use .Inner inside of the define block like this and I’m not sure how. I know this approach should theoretically work, though, since a short code that just defines some static block content works fine.

Or is there a better, less hacky way to do this?

Thanks in advance!

Please see the forum Requesting Help guidelines.

Okay! Thank you, sorry about that.

My project’s repository is a bit of a mess, so I set up a little demo repo that demonstrates what I’m trying to do. I hope that’s clear enough.

Base Template Blocks are placed in master templates and do not apply to Shortcodes.

It appears that you have mixed up these different templating concepts.

You would not define a secondary block in the baseof.html and then include a secondary.html shortcode.

I’m afraid that I do not currently have the time to explain the process in full, perhaps someone else can have a further look.

Try this:

git clone --single-branch -b hugo-forum-topic-37077 https://github.com/jmooring/hugo-testing hugo-forum-topic-37077
cd hugo-forum-topic-37077
hugo server

Test 1

Create your content as a page bundle, and place the secondary content in a page resource.

content/tests/test-1/
├── index.md      <-- page
└── secondary.md  <-- page resource

This is rendered by layouts/tests/test-1.html

Test 2

Create your content as a regular page, and define the secondary content with a shortcode.

content/tests/
└── test-2.md     <-- page

The shortcode looks like this:

{{ .Page.Scratch.Set "secondary" (.Inner | .Page.RenderString) }}

This is rendered by layouts/tests/test-2.html

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.