RSS template locations

I made some changes to the main rss template to add two requirements.

  1. show title + subtitle within
  2. always include the header image at the start of the post item
  3. filter out content that has “Params.options.unlisted” set to true

I first placed the code within layouts/rss.xml and noticed that the feeds for taxonomy were still using the internal template. Or at least they were still showing unlisted content.

So I copied the template to

  • layouts/_default/section.rss.xml
  • layouts/_default/taxonomy.rss.xml
  • layouts/_default/terms.rss.xml

From what I saw in the internal template, the file at layouts/rss.xml should also work for taxonomies and terms.

Is the code wrong or am I wrong in where I’m placing the template file? I’d like to avoid duplicating the template.

<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>{{ .Site.Title }} by {{ .Site.Params.author }}</title>
		<link>{{ .Permalink }}</link>
		<description>{{ .Site.Params.subtitle }}</description>
		<generator>Hugo - gohugo.io</generator>{{ with .Site.LanguageCode }}
		<language>{{.}}</language>{{end}}{{ with .Site.Author.email }}
		<managingEditor>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</managingEditor>{{end}}{{ with .Site.Author.email }}
		<webMaster>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</webMaster>{{end}}{{ with .Site.Copyright }}
		<copyright>{{.}}</copyright>{{end}}{{ if not .Date.IsZero }}
		<lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>{{ end }}
		<image>
			<url>{{ .Site.Params.logo | absURL }}</url>
			<title>{{ .Site.Title }}</title>
			<link>{{ .Permalink }}</link>
		</image>
		{{ with .OutputFormats.Get "RSS" }}
		{{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
		{{ end }}

{{- $pctx := . }}
{{- if .IsHome }}{{ $pctx = .Site }}{{ end }}
{{- $pages := slice }}
{{- if or $.IsHome $.IsSection }}
  {{- $pages = $pctx.RegularPages }}
{{- else }}
  {{- $pages = $pctx.Pages }}
{{- end }}
{{- $limit := .Site.Config.Services.RSS.Limit }}

{{/* New condition: Filter pages where .Params.options.unlisted is not true */}}
{{- $pages = where $pages "Params.options.unlisted" "!=" true }}

		{{ range first $limit $pages  }}
		<item>
			<title>{{ .Title }} {{.Params.subtitle}}</title>
			<link>{{ .Permalink }}</link>
			<pubDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
			{{ with .Site.Author.email }}<author>{{.}}{{ with $.Site.Author.name }} ({{.}}){{end}}</author>{{end}}
			<guid>{{ .Permalink }}</guid>
			<description>
				{{ $img := (.Resources.ByType "image").GetMatch "*header*" }}
				{{ with $img }}
				{{ $img := .Resize "640x" }}
				{{ printf "<![CDATA[<img src=\"%s\" width=\"%d\" height=\"%d\"/>]]>" $img.Permalink $img.Width $img.Height | safeHTML }}
				{{ end }}
				{{ .Content | html }}
			</description>
		</item>
		{{ end }}
	</channel>
</rss>

First, the template above throws an error unless you explicitly set services.rss.limit in your site configuration. The default value for services.rss.limit is -1, which you cannot pass to the limit function.

Second, this is the lookup order for RSS template:
https://gohugo.io/templates/rss/#template-lookup-order

You can see that layouts/rss.xml is the third on the list so… you’re fine.

Finally, I just copied the embedded RSS template and added one line before the limit check:

{{- $pages = where $pages "Params.options.unlisted" "ne" true }}

Works great.

That’s the same approach I took, and it does work to filter the content.

The issue comes up when the only file is layouts/rss.xml and the user visits site.com/post. In this case, Hugo uses the internal template and not the one in the layouts/ dir.

Sorry, I misunderstood. Take a look again at the lookup order. Which template is used by home, section, taxonomy, and term pages?

Right now, Home is using my template from layouts/rss.xml, all others use the internal hugo template.

From the lookup order page there isn’t a file that can be shared by all four options (home,section,taxonomy,term). So I don’t see any other option other than duplicating the code.

layouts/_default/rss.xml
layouts/_default/list.rss.xml
layouts/_default/list.xml

The list above is sorted by specificity, most specific at the top, just like the list in the documentation.

I tested layouts/_default/list.xml; no problems.

You’re right, at first it wasn’t working but after restarting the server it began showing the correct template.

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