CaRP Evolution Box
Web This Site

CaRP: Caching RSS Parser - Documentation


CaRP Interactive FAQ
Getting Started: Free Download | Purchase | Install
Reference: Functions | Plugins | Themes | Full Index
Etc.: Display Formatting | Example Code | Affiliates

JavaScript Output Example

Converting RSS to JavaScript:

If you are already exporting an RSS feed and would like to make it accessible to more webmasters, you can use CaRP to convert it to a JavaScript. This will enable websites that don't have the capability to import RSS newsfeeds or whose webmasters don't know how to do so to easily import your newsfeed with a single line of HTML code.

Be aware that since the remote website will not cache your JavaScript formatted feed (which they would hopefully, though not necessarily, be doing if they import your RSS formatted feed), this method will use more of your server's bandwidth to serve up your newsfeed to each person who visits their site. For this reason, we strongly recommend urging webmasters subscribing to your newsfeed to use a caching RSS parser, like CaRP, whenever possible.

To use CaRP to convert a newsfeed to JavaScript, create a PHP file like the following:

<?php
require_once "/www/shared/carp.php";
 
CarpConf('outputformat',1);
CarpConf('maxitems',10);
CarpConf('linktarget',1);
 
CarpShow('/rss/headlines.rdf','headlines.js');
?>

It will then be possible to import your newsfeed into any webpage by adding HTML code like the following to that page:

<script src="http://yourwebsite.com/ThePHPFileCreatedAbove.php"></script>

In the preceding example, the person importing your newsfeed has no control over it's appearance. By creating a more complex PHP page on your site, you can allow them to decide how many news items to display, as well as a number of other formatting options. Notice how this code provides default values in case the webmaster doesn't specify anything.

Note that no cache file is specified in the call to CarpShow. This is important, because the formatting may be different each time this page is loaded. Since you are formatting a local RSS feed in this case, this will not affect performance the way it would with a remote newsfeed.

require_once "/www/shared/carp.php";

function GetParam($name,$default='',$isNum=0) {
   $val=isset($_GET[$name])?$_GET[$name]:$default;
   if ($isNum) $val+=0;
   else $val=preg_replace("/[^a-zA-Z0-9. ]/",'',$val);
   return $val;
}

$mi=GetParam('mi',15,1);
$hbw=GetParam('mi',0,1);
$hbc=GetParam('hbc');
$hc=GetParam('hc');
$nw=GetParam('nw');

CarpConf('outputformat',1);
if (($doClass=strlen($hbc))||$hbw) {
   CarpConf('bilink','<div '.
      ($doClass?"class=\"$hbc\" ":'').
      ($hbw?"style=\"width:$hbw;":'').
      ($doClass?'':(';background:#ccc;padding:2px;border:1px solid #333;')).
      ($hbw?'"':'').
   '>');
   CarpConf('ailink','</div>');
}
CarpConf('maxitems',$mi);
if (strlen($hc)) CarpConf('ilinkclass',$hc);
CarpConf('linktarget',$nw);

CarpShow('/rss/headlines.rdf','');
?>

The following HTML code will import a newsfeed from the above PHP file showing a maximum of 8 items, no description fields, using a CSS class defined by the subscribing website (myHead) to set the formatting of the links. Webmasters will be able to set these and other options enabled by your PHP code.

<script src="http://yoursite.com/PHPFile.php?mi=8&hc=myHead"></script>

To help webmasters configure the appearance of your newsfeed, you may also wish to provide a file like the following:

<html><head><title>Format newsfeed</title>
<script>
function UpdateScript() {
   u='';
   df=document.f;
   if (df.mi.value!=15) u='?mi='+df.mi.value;
   if (df.hc.value.length)
      u=u+(u.length?'&':'?')+'hc='+df.hc.value.replace(' ','+');
   if (df.hbc.value.length)
      u=u+(u.length?'&':'?')+'hbc='+df.hbc.value.replace(' ','+');
   if (df.hbw.value!=0)
      u=u+(u.length?'&':'?')+'hbw='+df.hbw.value;
      
   nw=Number(df.nw[df.nw.selectedIndex].value);
   if (nw) u=u+(u.length?'&':'?')+'nw='+nw;
   
   df.s.value='<'+'script src="http://yourwebsite.com/PHPFile.php'+
      u+'"></scr'+'ipt>';
}
</script>
</head><body bgcolor="white">

<b>Javascript</b>:<br>
<form name="f" action="javascript:a=1;" method="post">
Add this HTML to your web page:<p>
<textarea name="s" rows="2" cols="80" wrap="virtual">&lt;script
   src="http://yourwebsite.com/PHPFile.php"&gt;&lt;/script&gt;</textarea><p>

To customize the formatting, use the following form:<br>
Maximum items to display:
   <input name="mi" size="3" value="15" onKeyUp="UpdateScript();"><br>
Headline display:<br>
   &nbsp;&nbsp;&nbsp;CSS class for link:
      <input name="hc" size="10" onKeyUp="UpdateScript();">
      (optional - defined in your stylesheet)<br>
   &nbsp;&nbsp;&nbsp;CSS class for box surrounding headline:
      <input name="hbc" size="10" onKeyUp="UpdateScript();">
      (optional - defined in your stylesheet)<br>
   &nbsp;&nbsp;&nbsp;Width of box surrounding headline:
      <input name="hbw" size="4" onKeyUp="UpdateScript();">
      (optional)<br>
When user clicks headline, follow link in:
   <select name="nw" onchange="UpdateScript();">
   <option selected value="0">the current browser window
      (and frame, if applicable)
   <option value="1">a new window
   <option value="2">the top frame of current browser window
   </select>
</form><p>

<b>RSS</b>:<br>
The address of the RSS newsfeed is http://yourwebsite.com/headlines.rdf

</body></html>