Custom Section RSS/Atom


I’m trying to create a custom RSS/Atom format for one of my Sections - in this case articles.

The code I’m trying to use is:

  {{ range first 10 .Data.Pages }}<entry>
      <title />
      <link href="{{ .Permalink }}"/>
      <id>{{ .Permalink }}</id>{{ with .Site.Params.Author }}
      <published>{{ .Date.Format "2006-01-02T15:04:05-07:00" | safeHTML }}</published>
      <updated>{{ .Lastmod.Format "2006-01-02T15:04:05-07:00" | safeHTML }}</updated>
      {{ `<content type="html"><![CDATA[` | safeHTML }}
      {{ slicestr 0 200 .Summary }} [...] <a href="{{ .Permalink }}">{{ .Title }}</a>]]></content>
    </entry>{{ end }}

but this fails with:

ERROR 2018/05/31 19:23:11 Error while rendering "Articles" in "":      
html/template:theme/article/rss.xml:12:51: {{range}} branches end in different contexts: 
{stateRCDATA delimNone urlPartNone jsCtxRegexp attrNone elementTitle <nil>}, {stateText 
delimNone urlPartNone jsCtxRegexp attrNone elementNone <nil>}

and I have no idea where to take this next. Anyone care to offer a suggestion?

Answering my own question:

The error was because of my use of <title />

This is unfortunate, as indicating an empty element by the use of the <item /> is permissible in the ATOM spec and in XML generally. Anyway, changing to <title></title> fixed the problem.


Thanks for posting this solution :slight_smile: , I got the same error today and remembered your post.

By the way, this Hugo error appears in much more situations than creating a custom feed. I got it today when inserting a SVG icon into my template.

Here’s the code for that icon:

<svg data-name="Layer 1" id="Layer_1" viewBox="0 0 64 64" xmlns=""><defs><style>.cls-1{fill:none;stroke:#ffb90b;stroke-linecap:round;stroke-linejoin:round;stroke-width:2px;}</style></defs><title/><polygon class="cls-1" points="39.6 8 22.08 8 20.08 35 28.14 35 23.39 56.38 43.92 26 34.66 26 39.6 8"/></svg>

Hugo errors on that <title/> portion:

Template changed "C:\\site\\themes\\theme2018\\layouts\\shortcodes\\tip.html": WRITE
ERROR 2018/06/05 09:52:35 error processing shortcode "theme/shortcodes/tip.html" for page "": html/template:theme/shortcodes/tip.html: ends in a non-text context: {stateRCDATA delimNone urlPartNone jsCtxRegexp attrNone elementTitle <nil>}
ERROR 2018/06/05 09:52:35 Failed to rebuild site: logged 1 error(s)

I got that SVG code from an icon generator so I assume it’s valid.

Should we improve Hugo’s behaviour in these situations? I think we should. At least with a more descriptive error than “ends in a non-text context: {stateRCDATA delimNone urlPartNone jsCtxRegexp attrNone elementTitle }” would be better I think.

1 Like