Showing latest post in a special template

Hi —

New Hugo user here. I was building an archive for my mailing list and ended up using Hugo. Loving it so far.

One problem: I wanted my latest post to render both in the normal archive (ex: https://craigmod.com/roden/012/, but also in a special template that would strip javascript and add some new tags specifically for my mailing list software (ex: https://craigmod.com/roden/latest/). But I didn’t want to duplicate the content, I didn’t want to have to copy and paste the content into /content/012.md and /content/latest.md.

Perhaps the solution to this is obvious to veteran users, but it wasn’t to me, and I didn’t see any other post on the forums or in the documentation addressing this.

I finally realized you can create a /content/latest.md post WITH ONLY FRONT MATTER. This was a huge revelation. And in that front matter, you can designate a special layout. So my /content/latest.md is just:

+++
layout = "latest"
+++

And in my /layouts/_default/latest.html, something like:

<HTML>
<head>
{{- range first 1 .Site.Pages }}
<title>{{ .Title }}</title>
</head>
<body>
<h1><singleline>{{ .Title }}</singleline></h1>

  {{ .Content }}

</body>
{{- end }}
</html>

(The layout’s a little more complicated than that but you get the drift.)

The insight was being able to make an empty content file to serve as a placeholder on the server, and then filling it in dynamically via /layouts, independent of the content file. WHOA.

I’m not even sure if this explanation does the insight justice, and chances are, there is a simpler way of doing this that I somehow missed in my information hunt.

But! In the case someone else is trying to create unique layouts that duplicate content without wanting to copy-and-paste the content, I’m posting this example to hopefully help if needed.

Hugo is a very curious little beast — deceptively broad and deep in capability. Haven’t had so much fun playing with web related stuff like this since the early 2000s.

Many thanks.

1 Like