How to render an external link in a Page list

Recently I had to add an external link to a Page list.

Here is a quick tutorial.

The file that needed to point to an external link has the following frontmatter:

+++
title = "Title"
external	= "https://www.example.com"
[_build]
render = "never"
local = true
publishResources = false
+++

In the above thanks to Hugo’s Build Options the content file will not get its own .Permalink due to render = "never" yet it is included as part of the Page list of the corresponding .Section thanks to local = true.

Then in the list.html template I am rendering the external link with something as simple as this:

<a href="{{ with .Params.external }}{{ . }}" target="_blank"{{ else }}{{ .Permalink }}"{{ end }}>

If the .Params.external exists then its value is rendered as the href attribute of said article (along with a target="_blank" attribute) otherwise a content file’s .Permalink value will be used.

3 Likes