I’m working on updating to version 3 of Eleventy (finally) and running into issues with my RSS feeds. I’ve finally got it building again, but it seems very hacky and I’m not sure it covers all potential issues.
Basically, I have lots of different page types that have intro/outro sections (using front matter data). I want these intro/outros included in the RSS feed, but they aren’t by default because they’re macros, shortcodes, or partials included in layouts, not part of the individual pages’ contents.
This is what I have after a lot of troubleshooting:
<content type="html">
{%- if (post.data.intro) -%}
{%- from "_macros/_post-intro.njk" import postIntro -%}
{%- set intro -%}{{ postIntro(post.data.intro) }}{%- endset -%}
{{ intro | escape }}
{%- endif -%}
{{ post.content | renderTransforms(post.data.page, metadata.base) }}
</content>
It makes a valid RSS feed, but I have 2 concerns:
- It will get very clunky as I have more and more different intro/outros.
- I’m not sure if any relative links in my intros/outros will be properly transformed so they work coming from an RSS reader. I tried applying renderTransforms to the intro, like it is used for post content, but that breaks (
Tried to use templateContent too early
). On previous versions of the RSS plugin, I just used htmlToAbsoluteUrls
on intros/outros, but that’s now deprecated.
Is there a better way to be doing this? If not, can I improve what’s here? It feels so weird and fragile, and constantly requires updating if I add more intros/outros. I guess I don’t understand why there isn’t an easier way to include info added via layouts to an RSS feed.
Cautiously, this seems like it’s the best way to do it, but it does bring a question to my mind. What’s the purpose of having the intro to the post handled through the separate files rather than in the post’s original file or in the layout template itself?
Sorry if that question has a really obvious answer I’m missing.
For the actual posts/pages, the intro is added via the layout (so I don’t have to set it for every post that has one). But the RSS feed template only looks at the post content, so unfortunately things that are added via layouts don’t get included. But it could be I’m missing something!
Which template language are you using?
The RSS virtual template introduced to Eleventy 3.0 has a templateData
option, which functions the same way as a template’s front matter data. So perhaps you can make use of that option in some way and use the virtual template to create your RSS feeds
For example, you can create a layout template for your RSS feeds, add macros, shortcodes or partials for your intro and outro to that layout template, add the layout template to your includes folder, then create your RSS feeds though the virtual template and add templateData: { layout: "yourlayout" }
to the configuration of your RSS feed virtual template.
EDIT: I have not tried to add intro and outro to my RSS feed entries (yet), so I am not sure if this method works, so I am just suggesting ideas here. If this method doesn’t work, it seems like what you’re having now is the best option I can think of.
1 Like
Interesting! I’ll have to try that out; I saw the new virtual template stuff, but wasn’t really sure how it worked.
I’m also doing some other stuff in my current manual RSS templates (like custom titles and permalinks), but maybe I can get around that by adding more font matter data to those posts (or use eleventyComputed to add it automatically).