2013-01-20 16:55:35 +00:00
---
layout: post
title: "Add Flattr to Octopress"
date: 2013-01-20 11:35
comments: true
description: "how to add flattr button and flattr payment link to your octopress"
published: yes
categories:
- flattr
- octopress
- feed
---
2013-01-21 00:18:38 +00:00
**Update** add payment relation to header, thanks to [@voxpelli ](https://twitter.com/voxpelli )
2013-01-20 16:55:35 +00:00
In this article I will show how to add [{% img left https://flattr.com/_img/icons/flattr_logo_16.png 14 14 %}Flattr ](https://flattr.com/ )
to your [{% img left /favicon.png 14 14 %} octopress ](http://octopress.org ) blog and feed.
First of all add your flattr user name (also known as user id) to the
configuration:
```yaml _config.yml
# Flattr
flattr_user: YourFlattrName
```
To add a flattr button to the sharing section of your posts, add this template:
{% raw %}
``` html source/_includes/post/flattr_button.html
< a class = "FlattrButton" style = "display:none;"
title="{{ page.title }}"
data-flattr-uid="{{ site.flattr_user }}"
2013-01-21 00:18:38 +00:00
data-flattr-tags="{{ page.categories | join: "," }}"
2013-01-20 16:55:35 +00:00
data-flattr-button="compact"
data-flattr-category="text"
href="{{ site.url }}{{ page.url }}">
2013-01-21 00:18:38 +00:00
{% if page.description %}{{ page.description }}{% else %}{{page.content | truncate: 500}}{% endif %}
2013-01-20 16:55:35 +00:00
< / a >
```
{% endraw %}
and add the following javascript to your custom head.html
{% raw %}
``` html source/_includes/custom/head.html
{% if site.flattr_user %}
< script type = "text/javascript" >
/* < ![CDATA[ */
(function() {
var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
t.parentNode.insertBefore(s, t);
})();
/* ]]> */
< / script >
{% endif %}
```
{% endraw %}
Now include it in your sharing template:
{% raw %}
``` html source/_includes/post/sharing.html
< div class = "share" >
{% if site.flattr_user %}
{% include post/flattr_button.html %}
{% endif %}
...
< / div >
```
{% endraw %}
The result will look like this:
< div >
< a class = "FlattrButton" style = "display:none;"
2013-01-21 00:18:38 +00:00
title="{{ page.title }}"
data-flattr-uid="{{ site.flattr_user }}"
data-flattr-tags="{{ page.categories | join: "," }}"
data-flattr-button="compact"
data-flattr-category="text"
href="{{ site.url }}{{ page.url }}">
{% if page.description %}{{ page.description }}{% else %}{{page.content | truncate: 500}}{% endif %}
2013-01-20 16:55:35 +00:00
< / a >
< / div >
2013-01-21 00:18:38 +00:00
To make flattr discoverable by programs (feed reader, podcatcher, browser extensions...), a [payment relation link ](http://developers.flattr.net/feed/ ) is needed in html head as well as in the atom feed.
2013-01-20 16:55:35 +00:00
First add this (lengthy) template...
{% raw %}
2013-01-21 00:18:38 +00:00
``` html source/_includes/flattr_param.html
{% if post %}
{% assign item = post %}
{% else %}
{% assign item = page %}
{% endif %}
{% capture flattr_url %}{{ site.url }}{{ item.url }}{% endcapture %}
2013-01-20 16:55:35 +00:00
2013-01-21 00:18:38 +00:00
{% capture flattr_title %}{% if item.title %}{{ item.title }}{% else %}{{ site.title }}{% endif %}{% endcapture %}
2013-01-20 16:55:35 +00:00
2013-01-21 00:18:38 +00:00
{% capture flattr_description %}{% if item.description %}{{ item.description}}{% else index == true %}{{ site.description }}{% endif %}{% endcapture %}
{% capture flattr_param %}url={{ flattr_url | cgi_escape }}& user_id={{site.flattr_user | cgi_escape }}& title={{ flattr_title | cgi_escape }}& category=text& description={{ flattr_description | truncate: 1000 | cgi_escape }}& tags={{ item.categories | join: "," | cgi_escape }}{% endcapture %}
```
{% endraw %}
2013-01-20 16:55:35 +00:00
2013-01-21 00:18:38 +00:00
... then include it in your feed ...
2013-01-20 16:55:35 +00:00
{% raw %}
``` xml source/atom.xml
---
layout: nil
---
<?xml version="1.0" encoding="utf-8"?>
< feed xmlns = "http://www.w3.org/2005/Atom" >
< title > <![CDATA[{{ site.title }}]]> < / title >
...
{% for post in site.posts limit: 20 %}
< entry >
< title type = "html" > <![CDATA[{{ post.title | cdata_escape }}]]> < / title >
< link href = "{{ site.url }}{{ post.url }}" / >
< updated > {{ post.date | date_to_xmlschema }}< / updated >
< id > {{ site.url }}{{ post.id }}< / id >
2013-01-21 00:18:38 +00:00
{% if site.flattr_user %}
2013-01-21 06:46:02 +00:00
{% include flattr_param.html %}
2013-01-21 00:18:38 +00:00
< link rel = "payment" href = "https://flattr.com/submit/auto?{{ flattr_param }}" type = "text/html" / >
{% endif %}
2013-01-20 16:55:35 +00:00
< content type = "html" > < ![CDATA[
{{ post.content | expand_urls: site.url | cdata_escape }}
]]>< / content >
< / entry >
{% endfor %}
< / feed >
```
{% endraw %}
2013-01-21 00:18:38 +00:00
and in your head template:
{% raw %}
``` html source/_includes/custom/head.html
{% if site.flattr_user %}
< script type = "text/javascript" >
/* < ![CDATA[ */
(function() {
var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
t.parentNode.insertBefore(s, t);
})();
/* ]]> */
< / script >
{% include flattr_param.html %}
< link rel = "payment" href = "https://flattr.com/submit/auto?{{ flattr_param }}" type = "text/html" / >
{% endif %}
```
{% endraw %}
2013-01-20 16:55:35 +00:00
Because not (yet) all feed reader support this feature, you can add a dedicated flattr link.
Therefor create a new template:
{% raw %}
2013-01-21 00:18:38 +00:00
```html source/_includes/flattr_feed_button.html
2013-01-21 06:46:02 +00:00
{% include flattr_param.html %}
2013-01-21 00:18:38 +00:00
< a href = "https://flattr.com/submit/auto?url={{ flattr_param }}" >
2013-01-20 16:55:35 +00:00
< img src = "https://api.flattr.com/button/flattr-badge-large.png"
2013-01-21 00:18:38 +00:00
alt="Flattr this"/>
2013-01-20 16:55:35 +00:00
< / a >
```
{% endraw %}
Compared to the other button, this one will not require javascript, which isn't
always available in feed readers.
Finally add it your feed template:
{% raw %}
``` xml source/atom.xml
---
layout: nil
---
<?xml version="1.0" encoding="utf-8"?>
< feed xmlns = "http://www.w3.org/2005/Atom" >
< title > <![CDATA[{{ site.title }}]]> < / title >
...
{% for post in site.posts limit: 20 %}
< entry >
< title type = "html" > <![CDATA[{{ post.title | cdata_escape }}]]> < / title >
< link href = "{{ site.url }}{{ post.url }}" / >
< updated > {{ post.date | date_to_xmlschema }}< / updated >
< id > {{ site.url }}{{ post.id }}< / id >
2013-01-21 00:18:38 +00:00
{% if site.flattr_user %}
{% include flattr_param.html %}
< link rel = "payment" href = "https://flattr.com/submit/auto?{{ flattr_param }}" type = "text/html" / >
{% endif %}
2013-01-20 16:55:35 +00:00
< content type = "html" > < ![CDATA[
{{ post.content | expand_urls: site.url | cdata_escape }}
{% if site.flattr_user %} {% include flattr_feed_button.html %} {% endif %}
]]>< / content >
< / entry >
{% endfor %}
< / feed >
```
{% endraw %}
This will add a flattr button to each entry in your feed.
Preview:
2013-01-21 06:46:02 +00:00
{% include flattr_param.html %}
2013-01-21 00:18:38 +00:00
[{% img left https://api.flattr.com/button/flattr-badge-large.png Alt Flattr this %} ](https://flattr.com/submit/auto?{{ flattr_param }} )
2013-01-20 16:55:35 +00:00
That's all folks! I hope you will become rich by your flattr income.