Jump to content


Photo

RSS Feed Module


  • Please log in to reply
61 replies to this topic

#1 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3382

  • LocationAtlanta, GA

Posted 24 February 2011 - 11:47 AM

Given a PageArray of pages, this module will render an RSS feed from them. This is intended to be used directly from a template file.

Installation
Download the RSS feed module at the bottom of this message. To install, unzip and place the file in /site/modules/. Go into Admin > Modules and click "Check for new modules" at the bottom. Then click the "install" button for the "MarkupRSS" module. From there, you'll be able to configure several aspects of the module.

Usage
Create a template in ProcessWire and use the following example as a guide. See the actual .module file for more configuration options too.

<?php

// retrieve the RSS module
$rss = $modules->get("MarkupRSS");

// configure the feed. see the actual module file for more optional config options.
$rss->title = "Latest updates";
$rss->description = "The most recent pages updated on my site";

// find the pages you want to appear in the feed.
// this can be any group of pages returned by $pages->find() or $page->children(), etc.
$items = $pages->find("limit=10, sort=-modified");

// send the output of the RSS feed, and you are done
$rss->render($items);

This module is now included in the ProcessWire core.

#2 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 301 posts
  • 13

  • LocationBratislava

Posted 24 February 2011 - 11:58 AM

great! I need it a lot , i will try immediatly  ;D

#3 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 301 posts
  • 13

  • LocationBratislava

Posted 01 March 2011 - 09:37 AM

I used for two website and i cannot find any fault, it's perfect and easy to use, maybe you can add a function to cut some long text in itemDescriptionField because sometimes i could not have a summary text in my template in addition to body field and for feed it's good to have small text, but this is not a big problem.

#4 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3382

  • LocationAtlanta, GA

Posted 01 March 2011 - 11:23 AM

Thanks for your feedback. I've added an option to limit the length of the item description field. You can now set this in the module config screen. It will attempt to truncate to the closest punctuation within your max length, or at the closest space if no punctuation is close by. If you download the current ZIP file attached to the original message, that will have it (MarkupRSS-module3.zip).

#5 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,586 posts
  • 905

  • LocationVihti, Finland

Posted 02 March 2011 - 02:10 AM

This is great news and great to get more well coded and commented examples to help module development. Haven't tried yet, but if I read correctly it should be trivial to create normal template, but add in top check if $input->get->format == rss and render rss instead of normal template.

Show we could get urls like: www.domain.com/news/ (normal news listing) and www.domain.com/news/?format=rss (rss feed).

Other way to handle rss for whole site would be one rss-template: www.domain.com/rss/?page=321 or www.domain.com/rss/?template=news etc...

I think after little testing this should come with processwire as default module (or do we call them core modules?).

#6 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3382

  • LocationAtlanta, GA

Posted 02 March 2011 - 07:46 AM

Thanks, glad you like it so far. The examples you mentioned should work well. Though I would probably recommend using URL segments rather than GET vars, so that the template output can be cachable. (GET vars aren't cachable at the template level). For example, lets say that domain.com/news/ is a page that lists an index of the latest news items. In your template, you could have it detect a URL segment "rss", so that domain.com/news/rss/ displays an RSS version of it instead:

<?php
if($input->urlSegment1 == 'rss') {
    // display the RSS feed
    $rss = $modules->get("MarkupRSS"); 
    // ...     
    $rss-render($page->children("limit=10")); 

} else {
    // display the regular news list template output
    include("./news-list.php"); 
}

I agree that this should probably be a core module (/wire/modules/) so I will add it to the source and include an RSS feed example in the default site profile.



#7 apeisa

apeisa

    Hero Member

  • Moderators
  • 2,586 posts
  • 905

  • LocationVihti, Finland

Posted 02 March 2011 - 11:39 AM

Thanks, glad you like it so far. The examples you mentioned should work well. Though I would probably recommend using URL segments rather than GET vars, so that the template output can be cachable. (GET vars aren't cachable at the template level).


This is good to know and thanks for clarification. So if I have any GET vars then template is never cached?

#8 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3382

  • LocationAtlanta, GA

Posted 02 March 2011 - 12:04 PM

The template will be cached, but it might never have an opportunity to see the GET vars (except when regenerating the cache). Since the output is cached, it would deliver cached output and your code that looks at the value of a GET or POST var would only run when the cache is rebuilt. This is why I usually leave off template caching for any pages that need to examine GET, POST, COOKIE or SESSION vars. Examples would be search engines, forms, etc.

Btw, the template cache is always off when you are logged in. Template caching is applicable only to anonymous users visiting the site.

The reason why ProcessWire doesn't cache GET vars is because there is an infinite number of them possible. We don't want to make it possible for someone to fill up your hard drive with a million different cached variations of the same page with different GET vars.

On the other hand, ProcessWire will cache up to 999 URL segments or page numbers, for each page that is using them (when enabled for it's template). However, unless your page is slow to render, it's simpler just to leave off template caching when pages using it need to generate different output according to the request.

#9 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 301 posts
  • 13

  • LocationBratislava

Posted 24 June 2011 - 07:50 AM

hey Ryan, i'm using this module but now i need to change the title of each item to a custom title composed by the title of the page + a custom fixed text.
Now i'm using:
$rss->itemTitleField = "title";
i would like to do something like =>  title+'my text';

Thanks


#10 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3382

  • LocationAtlanta, GA

Posted 24 June 2011 - 07:57 AM

You can modify the titles of the items before sending them to $rss->render:

<?php

// retrieve the RSS module
$rss = $modules->get("MarkupRSS"); 

// configure the feed. see the actual module file for more optional config options.
$rss->title = "Latest updates";
$rss->description = "The most recent pages updated on my site"; 

// find the pages you want to appear in the feed. 
// this can be any group of pages returned by $pages->find() or $page->children(), etc. 
$items = $pages->find("limit=10, sort=-modified"); 

// add something to the item titles
foreach($items as $item) {
    $item->title = $item->title . " my text";
}

// send the output of the RSS feed, and you are done
$rss->render($items);


#11 Sevarf2

Sevarf2

    Sr. Member

  • Members
  • PipPipPipPip
  • 301 posts
  • 13

  • LocationBratislava

Posted 24 June 2011 - 08:17 AM

thanks

#12 neildaemond

neildaemond

    Sr. Member

  • Members
  • PipPipPipPip
  • 118 posts
  • 31

  • LocationHong Kong

Posted 01 December 2011 - 10:35 AM

I was just wondering if this module supports more than 1 rss feed per template. ie, could i do something like this?

$rss = $modules->get("MarkupRSS");
$rss2 = $modules->get("MarkupRSS");
$rss3 = $modules->get("MarkupRSS");
...

#13 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3382

  • LocationAtlanta, GA

Posted 01 December 2011 - 11:54 AM

Yes, you can do that. This is what's called a 'singular' module, meaning it returns a new instance on every $modules->get("MarkupRSS"). So you can  create as many instances as you need, and they don't share data with each other.

#14 neildaemond

neildaemond

    Sr. Member

  • Members
  • PipPipPipPip
  • 118 posts
  • 31

  • LocationHong Kong

Posted 01 December 2011 - 12:11 PM

thanks! my guess was yes.. but didn't want to have to find out the hard way~

...oh, and someone else might be wondering too ::)

(btw, only taking 1.5hrs to reply my post is quite amazing!)

#15 Soma

Soma

    Hero Member

  • Moderators
  • 3,403 posts
  • 1934

  • LocationSH, Switzerland

Posted 01 December 2011 - 03:39 PM

(btw, only taking 1.5hrs to reply my post is quite amazing!)


Hold on ... , usually he's much faster! ;D

@somartist | modules created | support me, flattr my work flattr.com


#16 neildaemond

neildaemond

    Sr. Member

  • Members
  • PipPipPipPip
  • 118 posts
  • 31

  • LocationHong Kong

Posted 01 January 2012 - 09:22 AM

My RSS Feeds seem to only show the title of the Item ( I used the above mentioned code to modify the title of the items). I noticed in MarkupRSS.module that the $defaultConfigData array has

itemDescriptionField => 'summary'

My pages don't have a summary field.

Is there a way to make my RSS feed show more than just a title? ie some of the $body? I tried changing the module code to

itemDescriptionField => 'body'

but it didn't work. Also, I edited the module without reloading it anyhow, could that have made it not work?

thanks,

#17 Marty Walker

Marty Walker

    Sr. Member

  • Members
  • PipPipPipPip
  • 347 posts
  • 165

  • LocationKatoomba, AU

Posted 01 January 2012 - 07:07 PM

Hi,

I tend to use more than just the body in my pages so I code my own (http://www.smd.net.au/rss/). It outputs the body and then my project images which matches the order on my site.

Here's what it looks like:

<?php header('Content-Type: application/rss+xml; charset=utf-8'); ?>
<?php echo "<?phpxml version=\"1.0\" encoding=\"utf-8\"?>\n"; ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>stillmovingdesign</title>
<link>http://www.smd.net.au/rss</link>
<description>RSS Feed</description>
<?php
$featured = $pages->get("/work/")->find("template=work, sort=sort, limit=999");
foreach($featured as $feature) {
$image = $feature->images->first();
$h = $image->height();
$w = $image->width();
$thumb = $image->size(310,0);
echo "<item>\n<title>{$feature->title}</title>\n<link>http://www.smd.net.au{$feature->url}</link><dc:creator>stillmovingdesign</dc:creator><guid isPermaLink=\"false\">http://www.smd.net.au{$feature->url}</guid>";
echo "<description><![CDATA[";
echo "<p>" . $feature->summary . "</p>";
	foreach($feature->images as $featuredimages) {
	$large = $featuredimages->width(650);
	echo "<img src='http://www.smd.net.au{$large->url}' title='{$feature->title}' alt='{$feature->title}' />\n";
	}
echo "{$feature->body}";
echo "]]></description>\n";
echo "</item>\n\n";
}
?></channel></rss>

As an aside I use twitterfeed to automatically tweet each job I publish to Twitter. It works really well.

Regards
Marty


#18 ryan

ryan

    Hero Member

  • Administrators
  • 5,980 posts
  • 3382

  • LocationAtlanta, GA

Posted 02 January 2012 - 11:21 AM

My RSS Feeds seem to only show the title of the Item ( I used the above mentioned code to modify the title of the items). I noticed in MarkupRSS.module that the $defaultConfigData array has


The item description field is actually configurable in the module settings. In the admin, go to Modules > Markup > RSS. Click on it to configure the various settings it can use. This sets the defaults for all instances of MarkupRSS. If you prefer, you can also change the settings at runtime for any individual instance like this:

<?php
$rss = $modules->get('MarkupRSS'); 
$rss->itemDescriptionField = 'body';
echo $rss->render($yourPages);


Also, thanks for the great example stillmovingdesign!

#19 neildaemond

neildaemond

    Sr. Member

  • Members
  • PipPipPipPip
  • 118 posts
  • 31

  • LocationHong Kong

Posted 02 January 2012 - 07:56 PM

Works like a charm, Thanks Ryan!

#20 ClintonSkakun

ClintonSkakun

    Jr. Member

  • Members
  • PipPip
  • 24 posts
  • 12

Posted 27 January 2012 - 07:48 AM

I noticed that itemDescriptionField gets tags stripped. It would be great if this was a boolean option for this. I usually set up my blog(or other RSS feeds) to show the full article so anyone can view the entire thing in their reader.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users