{{craftSnippets}}
Home Articles Plugins Starter theme Components on Github Gists 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

RSS feed component on github gists
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:
#twig component
If you want to get latest updates on Craft CMS tutorials and components, follow me on Twitter or subscribe to RSS feed.
Quick links for this article:
RSS feed component on github gists
Articles on blog:
  • Frontend testing for Craft CMS websites with Codeception and Cypress
  • Building reactive Craft Commerce product page with Sprig plugin
  • Dynamically generated PDF attachments for Freeform submissions
  • Using template hooks in Craft CMS
  • Alpine JS modal component for Craft CMS
  • Using template UI elements to extend Craft CMS control panel
  • Matrix within a Matrix - possible solutions for Craft CMS
  • Universal email template for Craft CMS
  • Creating attributes table from entry fields in Craft CMS
  • Namespacing forms in Craft CMS
  • Creating map-based navigation for Craft CMS
  • Placeholder image macro for Craft CMS
  • Building AJAX contact form with Craft CMS
  • Using incognito field plugin for Craft CMS
  • Email footer creator made with Craft CMS
  • Infinite scrolling and lazy loading with Craft CMS
  • Using Javascript in Twig templates with Craft CMS
  • Twig templating tips and tricks for Craft CMS
  • Basic SEO functionality for Craft CMS
  • Working with dates in Craft CMS templates
  • Working with SVG images in Craft CMS templates
  • Responsive and lazy-loaded youtube videos with Craft CMS
  • Debugging and inspecting Twig templates in Craft CMS
  • Creating article excerpts with Twig component in Craft CMS
  • Adding favicons to Craft CMS website
  • Truncating text with Twig macros in Craft CMS
  • Universal language switcher for Craft CMS
  • Read time macro for Craft CMS
  • Using attr() function to render HTML attributes in Craft CMS
  • Building dynamic, AJAX based pagination for Craft CMS
  • How to add Disqus comments to Craft CMS website
  • Ellipsis pagination component for Craft CMS
  • Converting email addresses into links using Twig macro
  • Breadcrumb created from URL for Craft CMS
  • Best developer-oriented Craft CMS plugins
  • Search autocomplete component for Craft CMS
  • RSS feed - template component for Craft CMS
  • Testing emails sent by Craft CMS using Mailtrap
  • Quick edit link - Twig component for Craft CMS
  • Filtering entries in control panel using Searchit plugin
  • Fetching routes into Twig templates in Craft CMS


Frontend testing for Craft CMS websites with Codeception and Cypress

Building reactive Craft Commerce product page with Sprig plugin

Dynamically generated PDF attachments for Freeform submissions

Using template hooks in Craft CMS

Alpine JS modal component for Craft CMS

Using template UI elements to extend Craft CMS control panel

Matrix within a Matrix - possible solutions for Craft CMS

Universal email template for Craft CMS

Copyright ©2023 Piotr Pogorzelski