ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Errors when RSS feed empty

January 20, 2011 9:23pm

Subscribe [4]
  • #1 / Jan 20, 2011 9:23pm

    ralph.m

    225 posts

    Just wondering how people set up an RSS feed to cope when there are no entries in the channel that is set to display in the feed.

    Ideally, I’d like to have a {if no_results} option, as shown in the code below, which shows a fallback entry if there are no posts; but rather than show the alternate content, I just get PHP errors (presumably because there are no channel posts). Have I made a mistake, or is this not possible?

    These are the errors:

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined variable: last_update
    Filename: rss/mod.rss.php
    Line Number: 403

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined variable: edit_date
    Filename: rss/mod.rss.php
    Line Number: 403

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined variable: entry_date
    Filename: rss/mod.rss.php
    Line Number: 403

    A PHP Error was encountered
    Severity: Warning
    Message: Cannot modify header information - headers already sent by (output started at /home/username/system/codeigniter/system/core/Exceptions.php:170)
    Filename: core/Common.php
    Line Number: 409

    http://www.mysite.com/ http://www.rssboard.org/rss-specification ExpressionEngine v2.1.3 http://expressionengine.com/ empty_feed Fri, 21 Jan 2011 12:02:25 GMT


    This is the RSS code:

    {preload_replace:master_channel_name="event-notice"}
    {exp:rss:feed channel="{master_channel_name}"}
    
    <?xml version="1.0" encoding="{encoding}"?>
    <rss version="2.0"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
        xmlns:admin="http://webns.net/mvcb/"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:content="http://purl.org/rss/1.0/modules/content/">
    
        <channel>
        
        <title>{exp:xml_encode}Site Title{/exp:xml_encode}</title>
        <link>http://www.mysite.com</link>
        <description>Subtitle</description>
        <dc:language>{channel_language}</dc:language>
        <admin:generatorAgent rdf:resource="http://expressionengine.com/" >
        
    {exp:channel:entries channel="{master_channel_name}" limit="10" dynamic_start="on" disable="member_data|pagination"}
    <item>
      <title>{exp:xml_encode}{title}{/exp:xml_encode}</title>
      <link>{title_permalink='events/notice'}</link>
      <guid>{title_permalink='events/notice'}#When:{gmt_entry_date format="%H:%i:%sZ"}</guid>
      <description><![CDATA[{event-notice-content}]]></description>
      <dc:date>{gmt_entry_date format="%Y-%m-%dT%H:%i:%s%Q"}</dc:date>
    </item>           
    {if no_results}
    <item>
      <title>{exp:xml_encode}A Default Welcome Title{/exp:xml_encode}</title>
      <link>http://www.mysite.com/</link>
      <guid>http://www.mysite.com/</guid>
      <description><![CDATA[ This is a welcome message that people will read if they come to the RSS feed and there are currently no posts.
    This is a welcome message that people will read if they come to the RSS feed and there are currently no posts. You can read more about our featured events on our <a href="http://www.mysite.com/features">features page</a>. ]]></description>
    </item>
    {/if}
    {/exp:channel:entries}
        
        </channel>
    </rss>
    {/exp:rss:feed}

    I found a workaround by removing the {if no_results} part, adding offset=“1”, making a dummy post that no one will ever see (added offset=“1” on the display page too) and added in a hardcoded Welcome post in the Feed. A bit convoluted, though.

    The reason I’d like a fallback is that the posts (which are for events) are deleted once the event has expired, so it would be nice to have a placeholder post in the RSS feed when there are no scheduled events.

    Thanks for any help.

  • #2 / Jan 21, 2011 3:01pm

    Barry Cogan

    291 posts

    Hi Ralph,

    You can use the {if empty_feed} conditional.  You can visit our documentation on the subject, subheading: Empty Feeds and Errors, for more specifics 😊

  • #3 / Jan 21, 2011 6:28pm

    ralph.m

    225 posts

    Ah brilliant, thanks Barry. I overlooked that one. I’ll give it a try. 😊

  • #4 / Jan 21, 2011 6:32pm

    Barry Cogan

    291 posts

    Let me know how it works out!

  • #5 / Jan 22, 2011 12:56am

    ralph.m

    225 posts

    OK, after some trial and error, I found a solution that seems to work. It throws some errors if I’m logged in—which I don’t understand—but seems to work fine for anyone else. So I guess the errors are not critical.

    I’ll post my code here, in case it saves anyone else some time, or unless someone can see why this code would throw errors.

    (Just to recap: what I wanted was a fallback RSS item in case there were no entries in the events channel that the feed features.)

    {preload_replace:master_channel_name="event-notice"}
    {exp:rss:feed channel="{master_channel_name}"}
    <?xml version="1.0" encoding="{encoding}"?>
    <rss version="2.0"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
        xmlns:admin="http://webns.net/mvcb/"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
        xmlns:content="http://purl.org/rss/1.0/modules/content/">
    
        <channel>
        
        <title>{exp:xml_encode}Site Name{/exp:xml_encode}</title>
        <link>http://www.mysite.com</link>
        <description>Mysite subtitle</description>
        <dc:language>{channel_language}</dc:language>
        <admin:generatorAgent rdf:resource="http://expressionengine.com/" >
        
    {exp:channel:entries channel="{master_channel_name}" limit="10" dynamic_start="on" disable="member_data|pagination"}
    <item>
      <title>{exp:xml_encode}{title}{/exp:xml_encode}</title>
      <link>{title_permalink='events/notice'}</link>
      <guid>{title_permalink='events/notice'}</guid>
      <description><![CDATA[{event-notice-content}]]></description>
      <dc:date>{gmt_entry_date format="%Y-%m-%dT%H:%i:%s%Q"}</dc:date>
    </item>{/exp:channel:entries}
    
    </channel>
    </rss>
    
    {if empty_feed}
    <?xml version="1.0" encoding="{charset}"?>
    <rss version="2.0">
    
        <channel>
        
        <title>{exp:xml_encode}Site Name{/exp:xml_encode}</title>
        <link>http://www.mysite.com</link>
        <description>Mysite subtitle</description>
        
        <item>
        <title>Fallback RSS entry Title</title>
        <description><![CDATA[ A description of Mysite and what you can expect to find here.
    There are currently no events listed, but you can read more about our site and what we do on our <a href="http://www.mysite.com/about">about page</a>. ]]></description>
        </item>
        
        </channel>
    </rss>
    {/if}
    {/exp:rss:feed}

    The errors are as follows:

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined variable: last_update
    Filename: rss/mod.rss.php
    Line Number: 403

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined variable: edit_date
    Filename: rss/mod.rss.php
    Line Number: 403

    A PHP Error was encountered
    Severity: Notice
    Message: Undefined variable: entry_date
    Filename: rss/mod.rss.php
    Line Number: 403

    A PHP Error was encountered
    Severity: Warning
    Message: Cannot modify header information - headers already sent by (output started at /home/username/system/codeigniter/system/core/Exceptions.php:170)
    Filename: core/Common.php
    Line Number: 409

  • #6 / Jan 22, 2011 2:01am

    Barry Cogan

    291 posts

    Hi Ralph,

    Does it still generate the PHP warnings when you use the {if empty_feed} pair?

  • #7 / Jan 22, 2011 2:12am

    ralph.m

    225 posts

    Yes, the errors in my previous post are generated by the code in that post, which includes

    {if empty_feed}
    ...
    {/if}

    (I assume that’s what you mean by the {if empty_feed} pair.)

    Not sure if I set it up correctly, but it only shows errors for the logged-in administrator.

  • #8 / Jan 23, 2011 5:06pm

    Greg Salt

    3988 posts

    Hi ralph.m,

    Would you mind submitting a bug report for these errors? Please provide a summary of what you are trying to do, the error message and also a link back to this thread.

    Cheers

    Greg

  • #9 / Jan 23, 2011 7:12pm

    ralph.m

    225 posts

    OK, thanks Greg. Done. (I had to do it twice, though, as the first effort was mostly empty—not sure why, except that I tried code tags. Sorry about that.)

  • #10 / Jan 24, 2011 9:13am

    Sue Crocker

    26054 posts

    Link to bug report: https://support.ellislab.com/bugs/detail/15086/

    Thanks for posting it, Ralph! You’ll receive an email when there is progress made on the bug.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases