{{craftSnippets}}
Home Articles Plugins Starter theme About
  • Home
  • Articles
  • RSS feed - template component for Craft CMS
Posted on Mar 18, 2019 by Piotr Pogorzelski

RSS feed - template component for Craft CMS

This Twig template component generates RSS feed and makes it discoverable by rendering proper link in the head section of website.

This component is based on example rss feed from Craft docs. If you need atom feed, you can modify atom example from Craft docs in the same way.

{% spaceless %}
{#  settings #}
{% set title = siteName %}
{% set channelDescription = 'description' %}
{% set descriptionHandle = 'intro' %}
{% set items = craft.entries.section('articles').limit(50) %}
{# feed #}
{% if craft.app.request.segments|last == _self %}
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>{{ title }}</title>
        <link>{{ siteUrl }}</link>
        <atom:link href="{{ craft.app.request.absoluteUrl }}" rel="self" type="application/rss+xml" />
        {% if channelDescription is defined %}<description>{{ channelDescription }}</description>{% endif %}
        <language>{{ currentSite.language }}</language>
        <pubDate>{{ now|rss }}</pubDate>
        <lastBuildDate>{{ now|rss }}</lastBuildDate>

        {% for item in items %}
            <item>
                <title>{{ item.title }}</title>
                <link>{{ item.url }}</link>
                <pubDate>{{ item.postDate|rss }}</pubDate>
                <author>{{ item.author.email }} ({{ item.author }})</author>
                <guid>{{ item.url }}</guid>
                {% if item[descriptionHandle] is defined %}<description><![CDATA[
                    {{ item[descriptionHandle] }}
                ]]></description>{% endif %}
            </item>
        {% endfor %}
    </channel>
</rss>
{% else %}
{# link #}
<link rel="alternate" type="application/rss+xml" href="{{ url(_self) }}">
{% endif %}
{% endspaceless %}

Usage #

First, you need to drop this component into templates directory and name it feed.rss. It's important to give it .rss extension - thanks to that Craft will serve it with Content-Type HTTP headers set to application/rss+xml. Then, include component file into <head> section of your website.

{% include 'feed.rss' %}

RSS feed component works in two ways.

If if craft.app.request.segments|last == _self statement detects that component is accessed directly trough Craft template routing, (most likely by RSS reader) it will render feed.

If it is included as part of the page template, it will render a link to feed that will make it discoverable by readers. Thanks to that, you can keep all RSS related code in one file.

Settings #

Before starting using RSS feed component, you need to set up few things first:

  • title is by default set to siteName variable. If you don't want to make your internal site name public, change it to something else.
  • channelDescription is set to example string. If you want to, you can set it to global value or just comment it out - channel description won't be rendered if you do so.
  • descriptionHandle is the handle of entry field that each feed item description value should be taken from. If field with such handle doesn't exist, description won't be rendered.
  • items is craft element query returning elements you want in your feed. Don't forget to set proper limit parameter if you are dealing with website with a large amount of entries.

TAGS:
#Craft CMS 3 #Template components
Quick links for this article:
RSS feed component on github gists
Let the expert handle
your Craft CMS project.
70 Basic icons by Xicons.co
piotrpog@protonmail.com
Copyright © 2025 Piotr Pogorzelski
piotrpog@protonmail.com      LinkedIn profile      Github profile