This article includes a custom data source which allows RSS feeds to be treated as a data source. The items in the feed can be displayed using normal [Inline] ... [/Inline] and [Field] tags. Also includes an [RSS_Parser] data type.
In Lasso 8.5 data sources can be written entirely within LassoScript code. This tip includes a data source which parses RSS feeds and makes them available for display through normal [Inline] ... [/Inline] and [Field] tags.
Note - Lasso 8.5.5 or higher is required to run the code in this tip.
The code to display an RSS feed would be written like this. Note that the 'rss_datasource' is named in the -Host parameter. The -Database names the URL of the RSS feed. The only action supported by the data source is -Search. No -Table or other parameters are required.
[Inline(-Search,
-Host=Array(-datasource='rss_datasource'),
-Database='http://www.listsearch.com/lasso/rss.lasso')]
[Records]
<a href="[Field('link')]">[Field('title')]</a>
<br />[Field('pubdate')]
<br />[Field('description')]
<hr />
[/Records]
[/Inline]
The [Records] ... [/Records] tags loop through all the items in the RSS feed. The [Field] tags can be used to pull out each of the elements of the items. If necessary [Field_Names] can be used to list all the available field names.
Most RSS feeds contain meta-information which describes the feed and many include an image which should be displayed along with the feed. This meta information can be returned by specifying a nested inline with a -Search action and a -Table of 'channel' or 'image'.
The following code displays the image of the feed aligned to the right next to the name and description of the feed (and above the items). No [Records] ... [/Records] tags are necessary within the nested inlines since only one record is returned by each of these inlines.
[Inline(-Search,
-Host=array(-datasource='rss_datasource'),
-Database='http://www.listsearch.com/lasso/rss.lasso')]
[Inline(-Search, -Table='image')]
<a href="[field('link')]">
<img src="[Field('url')]"
align="right"
width="[Field('width')]"
height="[Field('height')]"
border="0" />
</a>
[/Inline]
[Inline(-Search, -Table='channel')]
<a href="[Field('link')]">
<h2>[Field('title')]</h2>
</a>
[Field('description')]
<hr />
[/Inline]
[Records]
<a href="[Field('link')]">[Field('title')]</a>
<br />[Field('pubdate')]
<br />[Field('description')]
<hr />
[/Records]
[/Inline]
The source file for the RSS data source can be downloaded using the following link. The "rss_datasource.lasso" file should be placed into either the global "LassoStartup" folder or a specific site's "LassoStartup" folder. The Lasso site must be restarted before the data source can be used.
RSS_Data_Source_and_Parser.zip
The RSS data source is a relatively simple data source. The only action it handles is -Search. It is not necessary to set up a host because the -Database specifies the URL of the RSS feed. This means that the data source can be used without any configuration in Lasso Administration. The -Table tag is optional and most other parameters like -SortField, -ReturnField, etc. are ignored.
The data source is defined as a custom type with a number of required member tags. The type name is then registered with Lasso using the [Datasource_Register] tags. When an inline is executed the member tags are called to return information about the data source and to perform the data source action.
The ->Action tag does all the heavy lifting of the data source. It creates an [RSS_Parser] type (see below) using the current [Database_Name] as the host. If no [Table_Name] is specified then the items of the RSS feed are returned using the [RSS_Parser->Items] tag. If the table is 'channel', 'image', 'cloud', or 'textinput' then the appropriate member tag of the [RSS_Parser] is used to determine the fields for a single record. The [Action_AddRecord] tag is used to add each record to the result set and the [Action_SetFoundCount] tag is used to set the [Found_Count].
The ->Info tag returns an array of field names. The [RSS_Parser->Fields] tag is used to generate an array of all the fields used in all the items in the current feed. The other -Table options simply return the corresponding [Map->Keys] array.
We don't want to fetch any RSS feed more than once on a given page so a simple caching mechanism is used to store each [RSS_Parser] type in a $__rss_datasource__ map with the URL of the RSS feed as its key.
The data source is rounded out by a -DatabaseNames tag which returns the URLs of all the cached RSS feeds and a ->TableNames tag which returns an array of table names including 'items', 'channel', 'image', 'cloud', and 'textinput'.
The [RSS_Parser] type is used internally by the RSS data source. It can also be called on its own to parse RSS feeds independent of the data source.
The [RSS_Parser] tag requires a -Host parameter which specifies the URL of an RSS feed. The feed is downloaded and parsed. Several member tags allow the elements within the RSS feed to be examined. If necessary, a -Port, -Username, and -Password can also be specified.
[RSS_Parser->Items] returns an array of maps, one for each item in the feed. The keys for each map include 'description', 'title', 'link', 'pubdate', etc. [RSS_Parser->Fields] is a utility tag which returns an array of all keys used in each of the maps returned by ->Items.
[RSS_Parser->Channel] returns a map with all of the channel meta-information for the feed. [RSS_Parser->Image] returns a map with all of the image meta-information for the feed. [RSS_Parser->Cloud] and [RSS_Parser->TextInput] work similarly.
Author: Fletcher Sandbeck
Created: 14 Dec 2007
Last Modified: 16 Mar 2011
Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.
©LassoSoft Inc 2015 | Web Development by Treefrog Inc | Privacy | Legal terms and Shipping | Contact LassoSoft