Kirby

Blog (RSS)

News about Kirby and stuff

Follow kirby on Twitter
@getkirby for more
updates.

Tags

How to add a RSS-Feed

Yesterday I wrote about how to build a blog with Kirby and today I'm going to show you how to add a RSS-Feed to your blog. This is applicable to pretty much any part of your Kirby-flavoured site, so if you want to rssify your site, read the next lines carefully :)

Adding an invisible feed page

Kirby

Do you remember our nice little page setup for our blog? We've got visible pages (with numbers prepended) for each article and a blog.txt so far. No we add an invisible feed folder, so later our feed will be available at http://yourdomain.com/blog/feed but it won't appear in any menu.

Inside the feed folder add a feed.txt, so we can build a specific feed.php template later.

Kirby

In your feed.txt you can add some basic information about your feed. Here's mine:

Title: Blog Feed
----
Description: The latest updates from our blog
---- 

Next up: the template

First of all please download the feed snippet, which I've prepared for you and add it to your site/snippets folder. It's no hokuspokus, just a simple wrapper to make feed generation easier. You can read more about it here

BTW: say hello to the brand new Kirby extension repository :)

With this snippet building the final feed template is very, very straight forward:

site/templates/feed.php

Add a file named feed.php to your site/templates folder. Do you remember that we named the text file inside our blog/feed folder feed.txt? By providing a feed.php template we make sure that this template will be used instead of the default.php template when we open http://yourdomain.com/blog/feed

All we need to do now is to get the right set of items for our feed and to embed our feed snippet. Here is how it is done:

<?php 

$articles = $pages->find('blog')->children()->visible()->flip()->limit(10);

snippet('feed', array(
  'link'  => url('blog'),
  'items' => $articles,
  'descriptionField'  => 'text',
  'descriptionLength' => 300
));

?>

In the first line we search for the latest articles in our blog. We find our blog page first, get all the visible children (all our articles), flip them to get the latest first and finally we make sure to only get the last 10 articles by using ->limit(10). Pretty easy, right?

Afterwards we embed our feed snippet. We can pass any number of additional variables to a snippet by defining an array of arguments as second parameter of the snippet() function. Let me explain the variables some more:

link

This is the main link in our feed, which takes the visitor back to our site. In this case we want them to get back to our blog, so we build an url to our blog with the url() helper function.

items

As items for our feed, we pass the set of $articles we found in the first line. The feed snippet will automatically take care of getting the right info out of those $articles (like title, url, etc.)

descriptionField

If you want to show a description for each item in your feed, you need to specify a field, which is available in any item and should be used for the description. For example every page of our blog has a text field, so we use that for the item description.

descriptionLength

This is maximum number of characters the description will have. An excerpt is automatically generated by the feed snippet. If you want to include the entire article instead of an excerpt in the feed, set descriptionLength to false.

More options

There are some more options available, which are not that important.
You can read more about them here

Title and description for your feed

Maybe you ask yourself where the title and description of your feed come from? Remember the feed.txt? We've added a title and a description field there and the feed snippet will automatically take that when generating your feed. You can define both as arguments for your feed snippet as well, which is described in the snippet docs

Visiting your feed

After all that hard work you should finally visit your feed and see if all went as expected. Go to http://yourdomain.com/blog/feed in your browser or feed reader.

Link to your feed

You probably want to link to your feed somewhere. The most simple way is to include a link in your blog template somewhere – maybe with a nice little RSS-icon. You can do that by using the handy url() helper function again:

<a href="<?php echo url('blog/feed') ?>">Subscribe to our RSS-feed</a>

Another good way is to add the feed to the header of your site, so browsers and feed readers can automatically detect it:

<link rel="alternate" type="application/rss+xml" href="<?php echo url('blog/feed') ?>" title="Blog Feed" />

If you want to make sure that the title="" attribute is filled dynamically with the content of the title field in your feed.txt, do it like this:

… title="<?php echo html($pages->find('blog/feed')->title()) ?>" …

Multiple feeds

With bigger site projects you probably get to the point where you need multiple feeds for different parts of your site. A good way to do this is by adding multiple feed templates. You probably would call the content file for your blog feed blogfeed.txt instead of feed.txt so your template for your blog feed would be blogfeed.php. Then you can easily just add another feed for your projects for example by having a content folder content/projects/feed with a projectfeed.txt file and a projectfeed.php template file. By embedding the feed snippet over and over again your template code is staying very small and maintainable, so it's really up to you how many feeds you want to add.

‹ Back

blog comments powered by Disqus