Growing with the Web

Atom feed templates in Jekyll

Published
Tags:

This post presents the basic Liquid template and customisations I use for my Jekyll blog’s Atom feed.

Jekyll logo

The feed’s location in the site doesn’t matter too much but it will need to be linked in your default.html layout file.

<link href='home_url/site.atom' rel='alternate' title='site_title - Atom' type='application/atom+xml'>

Basic template

Parts that need to be modified to suit the site are marked with explainations in the variables section below.

---
layout: nil
---
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="text" xml:lang="en">site_title</title>
  <link type="application/atom+xml" href="atom_feed_url" rel="self"/>
  <link type="text/html" href="home_url_canonical" rel="alternate"/>
  <updated>{{ site.time | date_to_xmlschema }}</updated>
  <id>unique_feed_identifier</id>
  <author>
    <name>author_name</name>
  </author>
  <rights>copyright_details</rights>

  {% for post in site.posts limit:20 %}
  <entry>
    <title>{{ post.title }}</title>
    <link href="home_url{{ post.url }}"/>
    <updated>{{ post.date | date_to_xmlschema }}</updated>
    <id>home_url{{ post.id }}</id>
    <content type="html">{{ post.content | xml_escape }}</content>
  </entry>
  {% endfor %}
</feed>

Variables

Variable Description Example
atom_feed_url Fully qualified atom feed URL http://www.growingwiththeweb.com/site.atom
author_name The primary author/company of the site Daniel Imms
copyright_details Copyright details for your feed Copyright (c) 2012-2014, Daniel Imms; all rights reserved.
home_url The home URL excluding the / at the end http://www.growingwiththeweb.com
home_url_canonical The canonical (best) home URL, ideally include the / at the end http://www.growingwiththeweb.com/
site_title Title of the website Growing with the Web
unique_feed_identifier Some unique string to identify the feed, ideally include your URL http://www.growingwiththeweb.com/all_feed

Further customisations

Beefing up <content>

The main section that is usually customised is the <content> element which defines the content of a post/entry/article. If you’re using the excerpt_separator feature then {{ post.excerpt }} could be used in addition to a link so your users will need to click through to the site to read the entire post.

<content type="html">
  &lt;p&gt;{{ post.excerpt | xml_escape }}&lt;/p&gt;
  &lt;p&gt;&lt;a href="home_url{{ post.url }}"&gt;Read the full article&lt;/&gt;&lt;/p&gt;
</content>

Banner ads are another example of something that can be slipped into the <content> element easily.

External posts

I setup an external post system in my blog that allows me to add articles I write externally to the homepage and feed. If the external_url property exists in a post’s YAML header then it will link to the external URL instead of the internally generated page. The feed code for this is as follows:

{% if post.external_url %}
<link href="{{ post.external_url }}"/>
{% else %}
<link href="home_url{{ post.url }}"/>
{% endif %}
<content type="html">
  &lt;p&gt;{{ post.excerpt | xml_escape }}&lt;/p&gt;
  &lt;p&gt;&lt;a href="{% if post.external_url %}{{ post.external_url }}{% else %}home_url{{ post.url }}{% endif %}"&gt;Read the full article&lt;/&gt;&lt;/p&gt;
</content>

Comments

comments powered by Disqus
Like this article?
Subscribe for more!