Skip to content

Instantly share code, notes, and snippets.

@mimoo
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mimoo/c7c184bef5cd9f9d908a to your computer and use it in GitHub Desktop.
Save mimoo/c7c184bef5cd9f9d908a to your computer and use it in GitHub Desktop.
RSS Feed with CodeIgniter + Twig
rss.xml:
<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>your title</title>
<link>http://www.example.com/</link>
<description>your description</description>
<language>en-us</language>
<lastBuildDate>{{updated}}</lastBuildDate>
{% for post in posts %}
<item>
<title>{{post.title}}</title>
<author>David Wong</author>
<pubDate>{{post.date}}</pubDate>
<link>{{post.url}}</link>
<comments>{{post.url}}#comments</comments>
<description>
<![CDATA[
{{post.text|nl2br}}
]]>
</description>
</item>
{% endfor %}
</channel>
</rss>
in a controller:
public function feed(){
// load
$this->load->helper('url');
// get posts
$this->db->order_by('id', 'desc');
$query = $this->db->get('posts', 20);
$posts = $query->result();
// format
foreach ($posts as $post)
{
$web = url_title($post->title, '-', TRUE);
$post->url = 'http://www.example.com/article/'.$post->id.'/'.$web.'/';
$post->date = date('r', strtotime($post->date));
}
$data = array(
'updated' => $posts[0]->date,
'posts' => $posts
);
// output cached
$output = $this->twig->render('rss.xml', $data);
$this->output->set_output($output);
$this->output->cache(60); // 1 hour
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment