Skip to content

Instantly share code, notes, and snippets.

@emilniklas
Last active November 14, 2015 16:37
Show Gist options
  • Save emilniklas/22f7bd29deda6b1b0df4 to your computer and use it in GitHub Desktop.
Save emilniklas/22f7bd29deda6b1b0df4 to your computer and use it in GitHub Desktop.
RSS templating with Chalk
class Entry {
String title; // My Blog Post
String url; // http://example.com/posts/my-blog-post
String tag; // tag:example.com,2015-11-14:/posts/my-blog-post (http://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id)
DateTime updatedAt; // 2015-11-14
String summary; // A post about an important issue that needs discussing
}
import 'package:shelf/shelf.dart' as shelf;
routes(Router router) {
router.get('/feed', () async {
final List<Entry> entries = someMethodOfFetchingAllEntries();
final rssTemplate = await (template('rss')
..title = 'My RSS Feed'
..subtitle = 'An important RSS feed about stuff that matters'
..url = 'http://example.com/feed'
..author = 'John Doe'
..tag = 'feed:example.com,2015:/feed' // http://web.archive.org/web/20110514113830/http://diveintomark.org/archives/2004/05/28/howto-atom-id
..entries = entries);
return new shelf.Response.ok(rssTemplate.encoded, headers: {
'Content-Type': 'application/atom+xml; charset=UTF-8'
});
});
}
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>$title</title>
<subtitle>$subtitle</subtitle>
<link href="$url" rel="self" />
<updated>${entries.last.updatedAt}</updated>
<author>
<name>$author</name>
</author>
<id>$tag</id>
@for (entry in entries.reversed)
<entry>
<title>${entry.title}</title>
<link href="${entry.url}"/>
<id>${entry.tag}</id>
<updated>${entry.updatedAt}</updated>
<summary>${entry.summary}</summary>
</entry>
@end for
</feed>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment