On this page... (hide)
- About this document
- The plethora of standard variables
- Using If
- Using else
- Compare text with eq (equal) and ne (not equal)
- Compare numbers with == and
!= - PerlCode
- PerlCode in cruser.pl
- Basic Example 1 - Using isNewDate
- Basic Example 2 - Link to viewnews.cgi
- Advanced Example - Trim the Text Field and add Read More Link
About this document
This document is based on the original written by Mark Olson. Changes made in this version include:
- Formatting for this wiki
- Spelling corrections
- Some of the code examples rewritten
- Notes added for clarification
You can view the original at:
http://coranto.org/wiki/examples/MyGuideStyleCodes.htm
Dale Ray
The plethora of standard variables
- Subject
- Text
- User
- Date
- Category
- UserField_Email
- Year
- Month_Name
- Month_Number
- Weekday
- Day
- Hour
- Minute
- Second
- AMPM
- Time_Zone
- TwoDigitYear
- TwoDigitMonth
- TwoDigitHour
- TwoDigitDay
Using If
The code that follows will print the date at the top of the day's worth of posts.
<If: Sub: isNewDate>
<b><Field: Month_Name> <Field: Day> '<Field: TwoDigitYear></b>
</If>
Using else
<If: Field: UserField_Email>
<a href="<Field: UserField_Email>"><Field: User></a>
<If: Else>
<Field: User>
</If>
Compare text with eq (equal) and ne (not equal)
<If: Field: Subject eq "w00t, Coranto">
Don't we all love Coranto?
<If: Else>
Grr..
</If>
Compare numbers with == and !=
<If: Field: Day == 32>
Something isn't right here...
</If>
PerlCode
<PerlCode>
if( $Day > 3 && $Day < 21 ) {
$Day = $Day.'th';
}elsif( $Day % 10 == 1 ) {
$Day = $Day.'st';
}elsif( $Day % 10 == 2 ) {
$Day = $Day.'nd';
}elsif( $Day % 10 == 3 ) {
$Day = $Day.'rd';
}else {
$Day = $Day.'th';
}
</PerlCode>
The above example gives you dates like 1st, 2nd, 3rd, etc.
PerlCode in cruser.pl
Put the following in the style:
<Sub: fixthedate>
And the stuff below this, in cruser.pl (it tell you where).
sub fixthedate {
if( $Day > 3 && $Day < 21 ) {
$Day = $Day.'th';
}elsif( $Day % 10 == 1 ) {
$Day = $Day.'st';
}elsif( $Day % 10 == 2 ) {
$Day = $Day.'nd';
}elsif( $Day % 10 == 3 ) {
$Day = $Day.'rd';
}else {
$Day = $Day.'th';
}
}
Open cruser.pl in a text editor and look for the section that says:
- ** INSERT CUSTOM SUBROUTINES HERE **
# Below is the place to put in any custom subroutines you have. (If you don't
# know what this means, just ignore this section.)
Insert sub-routines that you use often into this section. You can then call the subroutine as outlined in the example given above.
When upgrading Coranto make sure you backup your cruser.pl file so you don't lose the custom routines you have placed in it.
Basic Example 1 - Using isNewDate
<If: Sub: isNewDate>
<b><Field: Month_Number>/<Field: Day>/<Field: Year></b>
<hr>
<b><Field: Subject></b><br>
<Field: Text><br>
<Field: Hour>:<Field: Minute> {<Field: User>}
Would print something like...
3/13/06
Your Subject
The stuff you entered into the text field.
3:15 {User Name Here}
Wow.... - Look at how much you've learned. Obviously I have no idea if you, or anyone is learning anything at all from this, but I can hope.
10|27 {mark}
Basic Example 2 - Link to viewnews.cgi
<Field: Date> - <Field: User><br>
<a class="nav" href="/cgi-bin/viewnews.cgi?id=<Field: newsid>" title="the link that never dies"><b><Field: Subject></b></a><br>
Would print html something like...
2:27 AM</a> » - User Name Here
<a href="/cgi-bin/viewnews.cgi?id=AAuVZZkyZFyEXXwWq>Subject Here</a>
That looks like:
2:27 AM</a> » - User Name Here
Subject Here
Look, this one has a link to viewnews (nonfunctional) in it that is generated by the PerlCode.
Advanced Example - Trim the Text Field and add Read More Link
Put the following code right ABOVE <Field: Text> in your style.
<PerlCode>
my $yourlength = 150;
if (length($Text) > $yourlength){
$yourlength--;
my $Backup = $Text;
$Text =~ s/<br>/>br</g;
$Text =~ s/<br \/>/>br \/</g;
$text =~ s/<[^>]+>//g;
$Text =~ s/>br \/</<br \/>/g;
$Text =~ s/>br</<br>/g;
if (length($Text) > $yourlength){
$Text = substr($Text,0,index($Text,' ',$yourlength));
$Text .= qq~<br><a href="/cgi-bin/viewnews.cgi?id=$newsid" title="more">more...</a>~;
}else{
$Text = $Backup;
}
}</PerlCode>
This following two paragraphs from the Coranto Overview by Mark Olson to clarify the above example for those who get to this page first.
This one is, more or less the fullnews feature. Once it finishes checks the length, it strips all the HTML out (except <br />), and checks the length again. If it was shortened too much, it restores the original $Text variable, and stops. However, if it still is longer than the minimum length, it will shorten it, and then link it to the viewnews.
You can create the same result, with the exception of the linebreaks staying, simply by using:
<PerlCode>
if(length($Text) > 150) {
$Text = SnipText($Text, 150);
$Text .= qq~<br><a href="/cgi-bin/viewnews.cgi?id=$newsid" title="more">more...</a>~;
}
</PerlCode>
where you want the snip.
Other methods of snipping or trimming a field use the Snipper Addon by Parahead and the built in snip function in Coranto.
