NewsPro Web Site: Advanced Usage

Advanced Usage

This applies only to the latest version of NewsPro.

This contains only a few customizations; for many more, or to ask about doing something that hasn't already been discussed, visit the NewsPro Forum, after reading the NewsPro FAQ.

This section focuses on more advanced ways to customize NewsPro. Something that's often requested is to have the date above all the different news items for that date; how to do this is explained below. Another example below is how to make a separate page for each news item, and have links to each news item on your main page.

First, a brief explanation of how the script works and how to customize it. The script has been designed so that all commonly customized areas are located within two files: npconfig.pl and ndisplay.pl. Only very advanced customizations should involve editing newspro.cgi. Note that npconfig.dat is a data file used internally that is not to be edited.

Npconfig.pl contains the customization routines for everything but the news style. This includes: the different fields in the database, the content of the advanced settings page, and the different fields on the Submit News page. Ndisplay.pl contains the routines for the news style (basically one routine, the DoNewsHTML subroutine).

In ndisplay.pl, the following variables are set by default: $newstext, $newsdate, $newsemail, $newsname, $newssubject, and $newstime. $newsdate is the regular, English date, while $newstime (which you will probably never use) is in the UNIX time format of seconds since 1970. If you edit the news style via the web, you can refer to these as <InsertNews> or <InsertName>. When editing ndisplay.pl directly, you must use the variable format, i.e. $newstext.

Reminder: if you ever edit ndisplay.pl, you must follow the instructions and insert #<ManualEdit> somewhere in that file, or else the file may become damaged.

One of the most common customizations is to insert your own "field" into the news. For instance: related links, sources, type of news, link to a fuller version of the article, etc. NewsPro includes three fields by default that are empty, and can be easily enabled. These are: $uservar1, $uservar2, $uservar3. You can use these for whatever you want.

How to enable the empty variables: Open npconfig.pl in a text editor. All you need to do is edit @formfields, which is near the middle of the file in sub SubmitFormFields. Add the names of the fields you want to the end of this (your choices are uservar1, uservar2, and uservar3). It is possible to add even more fields fairly easily, but to do that you have to edit the Split and Join subroutines as well. Anyway, here is an example of an edited @formfields:

@formfields = ('newsname','newsdate','newsemail','newssubject','uservar1','uservar2');

That's all you have to do to enable the variables! If this is all you do, your submit form will have another text box labelled uservar1 (and/or 2, and/or 3). This is perfectly functional, but you may want a more descriptive name. To do this, add lines like the following:

$FormFieldsName{'uservar1'} = "Related Link Title";
$FormFieldsName{'uservar2'} = "Related Link URL";

and the appropriate name will be shown on the Submit form.

Now that the variables are enabled, you can use them in ndisplay.pl and your news just like you'd use $newssubject. Here is an example of how to add a "Related Link" field. Note the "if" line; what this does is ensures that you've entered a related link before it tries to display one, as most likely not all your news items will have related links.

sub DoNewsHTML {
$newshtml = qq~
<p><strong><font color="#ff0000">$newssubject</font> </strong><small>Posted
$newsdate by <a href="mailto:$newsemail">$newsname</a></small><br>
$newstext
~;
if ($uservar1 ne "") {
$newshtml .= qq~ <b>Related Link</b>:<a href="$uservar2">$uservar1</a> ~;
}
$newshtml .= "</p>";
}

Using Perl's .= operator is very useful while editing news style; all this does is adds something to the end of a string. You can do just about anything you want with the user variables.

Now for an example of how to have the date written once above all the day's news. To do this, you must first change the date format to display only the date, not time. This can be done via the "Date Format" option in Change Settings. Change your date format so that the time is not displayed. The most common date format used with this modification is:

<Field: Weekday>, <Field: Month_Name> <Field: Day>, <Field: Year>

Of course, you can disable the year or weekday if you'd like, just keep time and time zone off. Then, replace DoNewsHTML in ndisplay.pl with the following. Of course, change it to reflect your desired news style.

sub DoNewsHTML {

# Makes $newshtml empty.
$newshtml = "";
# Begin by checking if the last date printed is the same as this date.
# This uses NewsPro's internal isNewDate subroutine
# If the current date is different from the last one, print it
if (&isNewDate) {
$newshtml .= qq~<h2><font color="#ff0000">$newsdate</font></h2>~;
}
# That's it for printing the date! Easy! Now just go on and print the news
# (without the date, of course). Notice the use of .= rather than =
$newshtml .= qq~ <p><strong>$newssubject</strong><small> Posted by
<a href="mailto:$newsemail">$newsname</a></small><br>
$newstext</p>
~;
}

As you can see, achieving something like this is quite simple. Note that the script always generates the HTML in order from most recent news to least recent news (exception: some of the dynamically generated viewnews.cgi pages, such as search, only generate a small subset of the news and don't necessarily do so in that order). Also, many sites with a lot of news like to have the date above all the day's news, but each news item would include the time of day it was posted. This too is possible, because the routine used to generate the date sets quite a few variables, such as $Hour, $Minute, $Second, and $Time_Zone. So, to achieve the style with the news above and the time with every news post, use the following to print out the news:

$newshtml .= qq~ <p><strong>$newssubject</strong><small> Posted by
<a href="mailto:$newsemail">$newsname</a> \@ $Hour:$Minute</small><br>
$newstext</p>
~;

Notice the @ sign is "escaped" as \@. That's what you have to do with many characters writing Perl.

The full list of date/time variables: $Second, $Minute, $Hour, $AMPM, $ActualMonth, $Month_Day (the date), $Week_Day (counting from 0: Sunday is 0, Saturday 6), $Year, and $Time_Zone.

Headlines on your main page, news on one separate page

To do this, edit sub DoHeadlineHTML in ndisplay.pl. Change your headline style to use a link like:

<a href="http://mysite.com/news.html#newsitem$newsid">$newssubject</a><br>

and then enable headlines via Advanced Settings. Now, simply include headlines.txt in your main page, and include news.txt on your dedicated news page.

Headlines on one page, each news item on its own separate page

The best method of doing this requires that you have the viewnews.cgi script installed and configured (you can edit the style of viewnews.cgi-generated pages by editing viewnews.tmpl). You must also enable Headlines via Advanced Settings. Edit DoHeadlineHTML in ndisplay.pl, and use a link like the following for your news:

<a href="http://mysite.com/path/viewnews.cgi?newsid$newsid">$newssubject </a><br>

Now, include headlines.txt on your news page. That's all!