Overview

viewnews.cgi is included with the Coranto distribution. It is used to display a single item from the Coranto database. To use it you call it with a URL from your browser.

h**p://www.somesite.com/coranto/viewnews.cgi?id=EEVkEFuAypYsqhCYJo

The newsid is the ONLY required parameter.

↑ Contents

Specifying the newsid

Since you probably don't remember the id that Coranto creates for each item in the database how do you get that weird looking sequence of letters?

Coranto supplies the newsid for you. This is done when the page that contains the link to viewnews is created. A specific Coranto field is replaced with the newsid when a style is used to build news. It is:

<Field: newsid>

↑ Contents

Uses

As stated earlier - viewnews.cgi is used to display a single newsitem. The id supplied in the URL is used to fetch the item from the database. The page that the user sees in the browser window is created dynamically. The script reads the data from the database file and formats it then it is sent by the server to the browser.

viewnews.cgi can be used anytime you want to be able to display a single item but do not require a static page to be saved on the server.

You can use viewnews.cgi for:

  • Displaying items from headline links
  • Displaying the 'full story' from a summary link
  • Display a printable version of an item

↑ Contents

Headline Link

Your style could contain this code:

<li><a href="url_to/viewnews.cgi?id=<Field: newsid>"><Field: Subject></a></li>

to build an HTML list of links displaying the subject of each item. When the link is clicked viewnews.cgi will display the entire item.

↑ Contents

Full Story Link

<p><Field: Subject><br><Snip 250: Field: Text><a href="url_to/viewnews.cgi?id=<Field: newsid>">Full Story</a></p>

This style will display the subject and the first 250 characters of the text field with a link to the 'Full Story'.

↑ Contents

Print Item

<a href="url_to/viewnews.cgi?id=<Field: newsid>&style=print&template=print">Print This Item Only</a>

If you want the viewers of your site to be able to open a printable version of a single item out of a list of items on a page you can use a link similar to the one above. This introduces the optional parameters of viewnews.cgi. It is possible to specify both the style and the template that viewnews uses when displaying your item. We will discuss this below.

↑ Contents

Styles and Templates

viewnews.cgi will use the default template and style is none is specified in the URL that call viewnews. This is the default template:

<html><head><title><Field: Title></title></head><body>
<Field: Content>
</body></html>

Like all templates in Coranto this is just a wrapper for the content generated by the style.

This is the default style:

<p><strong><Field: Subject></strong> -- Posted by <a href="mailto:<Field: UserField_Email>"><Field: User></a> on <Field: Date><br><Field: Text></p>

The template provides the framework for a valid web page. The style formats the news item as you specify. You can change both to suit your needs.

The item represented by the newsid specified will be processed through the style and the result will be inserted into the template. The final result is sent to your visitor's browser by the server.

You can use viewnews.cgi to format the same item in different ways by changing the style and template parameters.

You can completely customize the appearance of the item with your choice of template and style.

The viewnews-readme.txt file included with Coranto states:

VIEW NEWS README


To use a style other then the one defined in viewnews.cgi use a url like this:

viewnews.cgi?id=[newsid]&style=[style name]&tmpl=[template file]

[newsid] = the news items id.

[style name] = the styles name, like "Default News Style" (without quotes).

[template file] = the filename of the .tmpl file you want to use, like "viewnews" (without .tmpl extension and quotes).

An example url might look like:
viewnews.cgi?id=EpEApluEEyolDIQbHI&style=Default+News+Style&tmpl=news

↑ Contents

Print Item Revisited

Your page displays multiple items but you want your visitor to be able to print a single item if they wish to.

Create a template and style that will format the item as you wish it to be printed. This usually means in a very simple format.

Place code similar to this:

<a href="url_to/viewnews.cgi?id=<Field: newsid>&style=print&template=print">Print This Item Only</a>

in your style that is used to create the page with multiple items where you want the print link to be displayed.

When your visitor clicks on the link the item will be displayed using the template and style specified. They can then print this page out.

↑ Contents

Conditional Styles and Templates

If you are displying items from more than one category in a headline list, on a calendar, or in any view where you want to link to the full story using viewnews.cgi and want the items presented in different fashion based on the category the item is from (or other criteria) you can accomplish this by planning ahead. You can set up the style that creates the links to viewnews.cgi create links with a different style or template based on the criteria you choose.

Example

I have items from more than one category being collected by a profile/style combination to be displayed on a calendar. I want links to details from the calendar, but since they are different types of items the layout of the details and which fields are included should be different.

In my case the items are in different categories so at the top of my style I used this:

<PerlCode>
if ($Category eq "One") {
$thisStyle = "styleOne";
} elsif ($Category eq "Two") {
$thisStyle = "styleTwo";
} else {
$thisStyle = "defaultStyle";
}
</PerlCode>

Then where the link code goes use:

<a href="h**p://www.yoursite.net/coranto/viewnews.cgi?id=<Field: newsid>&style=<Field: thisStyle>">Some Text to Display Here</a>

<Field: thisStyle> is replaced with the value set in the perlcode. You can now call viewnews with a style appropriate to the item.

You could also handle changing templates in the same fashion.

↑ Contents

A Recommendation

I never use (well almost never) use a template with viewnews.cgi. I have edited my default viewnews template to be empty. I include the necessary html in the style. This allows you to use Coranto fields anywhere in the output.

Limitations

When displaying a news item with viewnews.cgi you can not use server side includes. The SSI directive in the style or template will not be processed by the server.

Further Reading

Style Overview
Templates Overview
Coranto tutorial part 3 - Profiles, styles and templates
Viewnews/Searchnews by Sumaleth

Discussion/Comments

Discuss this article in this forum thread.



Page last modified on April 05, 2009, at 07:42 PM