Implementing and Using Item Counters in a Coranto Style

Introduction

Why do I need a counter?

The flexibility of Coranto Styles is centered around the inclusion of conditional statements in the style code.

It is important to remember that Coranto processes the style once for each item included in the profile. This means that you must know where you are (beginning, middle, end) if you want to have certain things happen at the proper time in the build process.

Conditional statements allow you to choose which action occurs depending on the value of a field. When wrapping html around your news or articles you may want to open or close tags only before the first item and after the last item.

There is no built in counting mechanism in Coranto. To accomplish our goals we will use raw perl and the NewsCounter addon by Parahead.

↑ Contents

Subjects covered

  • Start and end tags to enclose a group of items
  • Using a counter to create links to static pages created by the MultiPage addon
  • Using variables created in PerlCode in your style
  • Building styles that can be reused by multiple profiles

Coranto Version and Addons Used

Your mileage may vary

This information in this article was tested on:

  • Coranto V1.24
  • MultiPage V 1.51
  • NewsCounter 0.3

Addons used for development

  • News Categories
  • Template Editor V1.01
  • Sort Orders

Stuff Making the Job Easier

  • X MultiSubmit V2.1
  • Modify News : Include Field

These addons increase the flexibility and power of Coranto. I once again thank all of the addon authors for their time.

↑ Contents

Introduction to PerlCode

Turning on raw perl in your styles

The file cruser.pl contains the setting for turning raw perlcode on and off.

Quote

 # ** RAW PERL IN NEWS STYLES ** 
# When editing news styles, it's possible to use raw Perl code
# enclosed inside print 'code here'; tags.
# This is a useful and powerful feature if you know Perl, but can
# also be slightly dangerous: it allows anyone who can gain access
# to an Administrator-level account to run arbitrary commands on
# your server. Set to 1 to enable this feature, 0 to disable.
$EnableRawPerl = 1;

End Quote

By default this is turned ON. If you try PerlCode in your styles and it isn't working load the cruser.pl file into a text editor and check that the $EnableRawPerl variable is set to 1.

Always backup your current cruser.pl before upgrading Coranto. If you have any custom settings in this file you may have to transfer them to the version of cruser.pl included in the newer version.

↑ Contents

The <PerlCode> tag

To include perl in your style you use open and close tags similar to html tags or Coranto style tags. The syntax is:<PerlCode>your code here</PerlCode>You can put any vaild perl between the open and close tags.This allows your styles to perform any conditional test that can be done with perl.

Using Perl Variables

You've got more fields than you knew you had!

Coranto magically lets you access variables that you create and manipulate inside the <PerlCode> tags. For example if you create a variable like this:

 <PerlCode>$yourVariable = "something";</PerlCode>

You can access it later in your style using:

 <Field: yourVariable>

Coranto will then substitute the value set inside the perlcode tags anywhere you use the style tag <Field: yourvariable>. This temporary tag can be used in other style conditional statements. You can also access the values that are stored in your Coranto fields inside the perlcode tags. For instance you can access the Subject field in your perl code by turning it into a variable name:

 <PerlCode>$Subject;</PerlCode>

↑ Contents

Using Hashes

Making your perlcode reusable

If two or more profiles use the same style when the items for each profile are built the variables you use will retain their values and cause unexpected results. To keep from having to think up unique names for variables and having multiple styles that are the same except for these names we can access the variable using perl hashes.

The format for this is:

$yourVariable{$ProfileName}

this makes the name of the current profile the key for the hash, so that as each profile is built the variables you use are unique for that profile. By using this technique you can use the same style for more than one profile without getting unexpected results from inherited variables.

Code Portability - Avoid Unexpected Conflicts

Musicvid makes the following point in a discussion in the Coranto Forum:

I wanted to make the distinction between using the same style with different profiles, --or-- defining the same variable in multiple styles. In that case, one must either name the variable uniquely in each style, or localize it with a "my" operator in the PerlCode. Otherwise, the code gets run every time any style or routine in Coranto sees the variable.

This can be especially dangerous when using a previously defined global variable such as $Text -- if you write PerlCode that says

 $Text = blahblah($Text);

in a style, for instance, it will run altered by sub blahblah every time the script sees $Text in any style from that point forward, and will really crack up your results if that is not what you intended. However if you say:

 my $Text = blahblah($Text);

it will stay plain old $Text everywhere except in the profiles that call that particular style.

I wanted to clarify for those who aren't as experienced in putting Perl in their styles.

Thanks to Musicvid for the clarification.

↑ Contents

Building the Counter

The Counter Code

Putting it to work

The basic code for the counter is:

if (not $counter) {
$counter = 1;
} else {
$counter++;
}
$itemCount = $counter;

↑ Contents

Code Analysis

if (not $counter)

tests if the counter variable has been initialized

$counter = 1;

sets it equal to one (1).

Each pass after that the statement after the else is executed:

$counter++;

and the counter is incremented by one (1).

You could use this to cause the html tags that you want to enclose your content to only be inserted on before the first item in your profile. For example if you want your content to be enclosed in a table you could insert the opening table tag before the first item using:

The opening tag for the table will only be inserted into the profile.txt file the first time that the style is processed.

You could also use the following code to place anything you need to do only once each time the profile is processed:

if (not $counter) {
$counter = 1;
your run once code here!
use to initialize variables, etc.
}

The code inside the if statement will only execute on the first pass through the profile.

↑ Contents

Finding the end of the item list

Closing your content container

To determine when you are at the last item in the profile you have to know how many items will be built when the profile is processed. The easiest way to do this is to use the NewsCounter addon by Parahead. From the addon documentation:

Please note that this addon work by replicating the core building process performed in Coranto.

Read the complete NewsCounter addon documentation to see all of the uses of this addon.To use the counter code and this addon together you must install and enable the NewsCounter addon. Our perl code at the start of our style now becomes:

<PerlCode>
$done = 0;
if (not $counter) {
$counter = 1;
} else {
$counter++;
}
$itemCount = $counter;
if ($itemCount == $NCActiveCount) {
$done = 1;
}</PerlCode>

if ($itemCount == $NCActiveCount) {
$done = 1;
}

This new if statement compares the counter with the hash created by the NewsCounter addon that holds the number of active items in the profile. If they are the same the variable $done is set to 1. We use the active count instead of the total count hash because the total count also counts items filtered out by time, number, or by other addons such as Filter by Field. At the end of your style you add:

<If: Field: done></table></If>

The closing tag for your content will be inserted only after the last news item. The first line:

$done = 0;

sets our flag to zero, it is set to one only when the if statement is true.

↑ Contents

A Real Life Demo

Hey it works!

Ok, let's get to a real life example using the counter to insert a header before the first news item and footer after the last item. Take a look at the first sample output Δ. This is a standard Coranto profile. It is filtered by number with the number of items limited to five (5) and the default reverse chronological order. Here is a second sample Δ. This profile is also a standard profile. It is filtered by number with the number of items set to four (4) and the sort order is chronological. Both profiles use the same style and template.

↑ Contents

Final Style Code

<PerlCode>
$done = 0;
$TotalCount = $NCTotalCount;
$ActiveCount = $NCActiveCount;
$ArchiveCount = $NCArchiveCount;
$NCTotalCount = '';
if (not $counter) {
$counter = 1;
} else {
$counter++;
}
$itemCount = $counter;
if ($itemCount == $ActiveCount) {
$done = 1;
}
</PerlCode>
<If: Field: itemCount == 1>
<If: Field: TotalCount>
<p>INFORMATION: In this profile there are a total of <Field: TotalCount> newsitems where <Field: ActiveCount> is active and <Field: ArchiveCount> is archived.</p>
</If>
<h1>Demo of Counter in Coranto Style</h1>
<div class="countdemo"><table class="countdemo">
</If><tr>
<td class="subject"><Field: Subject></td>
</tr>
<tr>
<td class="text"><Field: Text><p class="author">Posted by: <Field: User> on <Field: Date></p></td>
</tr><If: Field: done></table></div>
<h1>This could be your Footer</h1></If>

↑ Contents

NewsCounter Code

This code:

$TotalCount = $NCTotalCount;
$ActiveCount = $NCActiveCount;
$ArchiveCount = $NCArchiveCount;
$NCTotalCount = '';

is from the NewsCounter documentation. It shows how to use the NewsCounter hashes.

This code:

<p>INFORMATION: In this profile there are a total of <Field: TotalCount> newsitems where <Field: ActiveCount> is active and <Field: ArchiveCount> is archived.</p>

uses the NewsCounter variables to output information about the number of items in the profile.

↑ Contents

MultiPage Headline Links

Introducing MultiPage

Static pages with X items per page

MultiPage is an addon that is used to create static pages from a profile. After installing and enabling MultiPage from the Manage Profiles page in Coranto click on the MultiPage link and set the MultiPage options.

Screen Capture of Settings Δ - opens new browser window

MultiPage Link Settings

Navigating through the news items

You now need to set up the MultiPage links for navigating between news items. To provide wrap around navigation, that is the ability to jump from the last item to the first and from the first to go the last you need to get creative.

Use the Previous Page No-Link Code (A2): to jump from the first item to the last, like this:

<a href="h**p://www.example.net/count/countdemo-<MP: PageCount>.html">Last Item</a>

Using the MultiPage tag <MP:PageCount> causes the value for total number of pages to be inserted in this spot when the profile is processed. Type in the URL for your site in place of the one above.

Use the Next Page No-Link Code (B2): to enable jumping from the last item to the first, like this:

<a href="h**p://www.example.net/count/countdemo-1.html">First Item</a>

Screen Capture of Link Bar Settings Δ opens new browser window

↑ Contents

MultiPage Link Style

Using the counter to create valid links

Now that we have MultiPage set up we can create a style that builds links to the files. In this case each file has one item in it. The style to do this uses the counter that we have already seen. Here is the style code to build the headlines into an HTML list:

<PerlCode>
if (not $counter) {
$counter = 1;
} else {
$counter++;
}
$itemCount = $counter;
</PerlCode>
<li><a href="h**p://www.example.net/count/mp_countdemo-<Field: itemCount>"><Field: Subject></a></li>

In this case all we need is the counter. We will include the the text file with the links list into a page with SSI or PHP includes.The links at the bottom of the menu on the Table of Contents page was created with this style and test data.

↑ Contents

Counting by Twos (or Threes)

You are not limited to one item per page

So - you use MultiPage and have 3 articles per page and want to create a headlines file to link to the static pages created by MultiPage. Ok lets see what we can do. Here is our headline code for one item per page:

<PerlCode>
if (not $counter) {
$counter = 1;
} else {
$counter++;
}
$itemCount = $counter;
</PerlCode>

We will create a new variable to hold the number of items per page:

$ItemsPerPage

Now we need to set this to the number of items per page and add another loop that tests when the counter is a multiple of the items per page, plus 1. We need to count the articles one by one, but we only want to increment the page number when MultiPage would create a new file. Here is the revised perl code:

<PerlCode> \\ $newLink = 0;
$ItemsPerPage = 3;
if (not $counter) {
$counter = 1;
} else {
$counter++;
} \\ $itemCount = $counter;
if (($counter % $ItemsPerPage) == 1) {
$pageCount++; \\ $newLink = 1;
}
</PerlCode>
<If: Field: newLink><li><a href="h**p://www.yoursite.net/restofpath/yourfile-<Field: pageCount>.html">Page <Field: pageCount></a></li></If>

This style counts by three, but it can be used to count by any increment. Always set the value of $ItemsPerPage to the same number as the number of items in your MultiPage profile. The last if statement in the perl code uses the modulus operator (%). This operator returns the remainder of the division. So it prints a headline link each time the remainder is one (1).

↑ Contents

Further Discussion

Searching in the forums is often the best way to find solutions.

Disclaimer, Thanks, and Copyright

No warranty or guarantee!

DisclaimerI am providing this information to assist others in using Coranto.While I do the best I can to make sure the information is accurate I can not guarantee that I have not made a mistake. Make sure that you back-up your files and test these techniques on non-essential data.I am not a perl programmer. All ideas presented here have been discovered through my use of Coranto and discussions in the Coranto Forums.

Thanks I would like to thank some of the people that through their participation in the Coranto forums made this document possible:

  • Parahead
  • LoneOwl
  • kriko

Copyright

I can not claim copyright on the ideas that are discussed in this article. As I mention above this work is inspired by the discussion that goes on in the Coranto forums. I am responsible for the time and effort of putting these ideas in a form that can be shared by others.

Copyright/License

Dale Ray

Revisions, Corrections

  • January 16, 2009 - Revised copyright, fixed broken links
  • February 6, 2007 - Revised Copyright, fixed some typing and formatting errors
  • July 28, 2006 - Reformatted and published to this wiki
  • April 16, 2006 - Revised Chapter 3, Section 1 The Counter Code
  • April 16, 2006 - General revisions and corrections
  • December 15, 2004 - Typos corrected
  • July 9, 2004 - Corrected suggested link in Chapter 1, Section 2
  • July 5, 2004 - Chapter 5 added
  • July 3, 2004 - Original document published

↑ Contents

Categories:




Page last modified on October 20, 2010, at 04:25 PM