Where to keep re-usable code block?

Hi,

I converted my Google sites personal blog website to Hugo a couple of years ago and have a question. I’ve added a nav bar to my content pages that has links to next and previous pages (based on creation date) as well as to an image gallery if one is defined in the front matter (Params.album). I have this same snippet of code in 2 places, at the top and bottom of each post. I’d like to have this snippet exist only in one place and reference it from the places I use it so that I can maintain it in one location rather than needing to edit 2 duplicate locations every time I want to change it.

What Hugo feature would best support this? If it matters I am using a mildly modified Ananke theme and my repo is located here: GitHub - RoninTech/CarryOnRoundTheWorld: Paul and Helen's Website

The identical code blocks I am talking about are here (in the same file):

and

Here is an example page on the live site showing these nav sections at the top and bottom of the page:

Thanks for the help and for an amazing tool in Hugo!

It depends a little, but in general you have 2 options:

  1. Use block in your base template.
  2. Use partial templates.

Or a combination of the 2.

A little digression, and I don’t think you need this, but I pondered about something similar a few weeks back: A way for a template (e.g. single.html) to signal a flow in the base template · Issue #11023 · gohugoio/hugo · GitHub

1 Like

Thanks for the quick response! :slightly_smiling_face:

I’ve created a partial file outside the template in my layouts/partials folder. Seems to work well.

Thanks again.

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