Hugo

Link to RSS feed on all pages using OutputFormats

Hi all!

How is it possible to link to /index.xml (feed url may be changed) on all pages using this technique.

Currently the feed is linked on the landing page only. It would make sense to include the ”basic feed“ on all pages—maybe also section feeds.

Of course, you can just type out <link …>. Yet, I would like to automate it for re-usable templates and changed output formats for the RSS feed.

Thank you very much!

The RSS alternate output is enabled by default for home, section, taxonomy and taxnomyTerm pages (See Page Kinds).

So you simply need to add a code like this in your baseof.html (See Base Templates and Blocks):

{{ range .AlternativeOutputFormats -}}
    {{ printf "<link rel=\"%s\" type=\"%s+%s\" href=\"%s\" title=\"%s\" />" .Rel .MediaType.Type .MediaType.Suffix .Permalink (printf "%s for %s" (.Name | title) $.Site.Title) | safeHTML }}
{{ end -}}

Above is a slightly improved version of the snippet in docs.

Thanks a lot, kaushalmodi.

/index.xml still only shows up on the homepage.

On pages no alternate link is shown (unless you set page = ["HTML", "RSS"]); section pages link to the section feed, but not to the base feed.

Any other idea (or am I doing something wrong?)?

Correct. That is expected.

Note that I did not have “page” in the list I wrote earlier:

The RSS alternate output is enabled by default for home, section, taxonomy and taxnomyTerm pages

See Default Output Formats.

That is expected too… So you need the section pages to link to the feed in home page too? Then why generate the section feeds?

If you need to hard-code all pages to link to the home page feed, you need to hard-code that link in the templates.

Yes, that is correct. Actually everything works properly and as expected. I was just wondering if it was possible to have a link to /index.xml on all pages using range .OutputFormats. It is also responsivble for non hard coded canonical output. It would have been nice and out of the box to use it for feed and canonical link in all my templates.

Hard coding is no problem, of course.

Thanks a lot!

I came here trying to do the same thing (I think). I only have 2 feeds enabled for the whole site — RSS and JSON. Both are enabled for just the homepage. I want them in the head of every page, but I don’t want to hard code them. Here’s what I came up with:

{{- with .Site.GetPage "/" }}
    {{- range .OutputFormats }}
        {{ printf `<link rel="%s" type="%s" href="%s" title="%s" />` .Rel .MediaType.Type .Permalink $.Site.Title | safeHTML }}
    {{- end }}
{{- end }}