yet more snipping woes

Discuss news styles, css code, and other Coranto customizations in here...the possibilities are endless, so have at it!

Moderator: Spunkmeyer

yet more snipping woes

Postby Esstee » Fri Jun 03, 2005 12:39 am

I know this topic has been way overused on this forum but believe me when I
say I have searched hi and low for answers and its not easy to pinpoint
the correct code.

I need to snip headlines in a tight arrangement, every character counts so
I want to snip at an exact character no. right through the words and
add the ... afterwards.

I found a few snip threads but I couldn't find anything to do this
please help
User avatar
Esstee
 
Posts: 320
Joined: Thu Nov 25, 2004 4:13 pm
Location: Deep in some project

Postby Esstee » Fri Jun 03, 2005 1:32 am

Okay I found this:

http://coranto.org/forum/viewtopic.php?t=4191&highlight=snip+character

Might not be the best way to do it, but here's how I'm making a snipped headline on a site of mine, with 25 characters followed by "...". If you want a different number of characters, change both occurrences of 25 to something else. If you don't want the ..., remove it. If you want to snip a different field, replace $Subject with $Fieldname, where Fieldname is the name of the field in question, and if you feel like it rename $SnippedSubject as well. I've removed the Maginot stuff from my style in this demonstration to avoid confusion.

Code: Select all
<PerlCode>
if (length ($Subject) > 25) {
$SnippedSubject = substr($Subject,0,25);
}
</PerlCode><If: Field: SnippedSubject><Field: SnippedSubject>...<If: Else><Field: Subject></If><br />
<PerlCode>
{
 $SnippedSubject = "";
}
</PerlCode>


This will snip to an exact number of characters, regardless of whether there's HTML tags in it. If you are snipping a field that has HTML tags in it - say the news text field - , you will NOT want to do this as it might snip the text after an open tag, but before the closing tag - or even in the middle of an open or close tag. That would cause problems in your output, hence use the approach below:

Code: Select all
<PerlCode>
$StrippedText = HTMLstrip($Text);
if (length ($StrippedText) > 500) {
$SnippedText = substr($StrippedText,0,500);
}
</PerlCode><If: Field: SnippedText><Field: SnippedText>... <A HREF="<MaginotURL: news>">(More)</A><If: Else><Field: Text></If><br />
<PerlCode>
{
 $SnippedText = "";
}
</PerlCode>


This will check the length of your news Text field without HTML tags, and if it is longer than 500 characters will put the first 500 characters of that field without HTML, followed by a more link to a Maginot profile called news. If there is less than 500 characters, it will put the entire news text field, including all HTML tags.


For the sake of reference I am using the first segment, I don't quite
understand what the differences are between the two and I don't use
wysiwyg in my news submissions, if someone knows which would be
better in my case then please share your thoughts.
User avatar
Esstee
 
Posts: 320
Joined: Thu Nov 25, 2004 4:13 pm
Location: Deep in some project

Postby kriko » Fri Jun 03, 2005 8:11 am

Instead of:
Code: Select all
<PerlCode>
if (length ($Subject) > 25) {
$SnippedSubject = substr($Subject,0,25);
}
</PerlCode><If: Field: SnippedSubject><Field: SnippedSubject>...<If: Else><Field: Subject></If><br />
<PerlCode>
{
 $SnippedSubject = "";
}
</PerlCode>


The following can freely be used:
Code: Select all
<Snip 25: Field: Subject><br />
Kristjan aka. kriko
now a wireless expert
User avatar
kriko
 
Posts: 3328
Joined: Sat Feb 24, 2007 8:13 pm

Postby Esstee » Fri Jun 03, 2005 11:01 am

kriko wrote:Instead of:
Code: Select all
<PerlCode>
if (length ($Subject) > 25) {
$SnippedSubject = substr($Subject,0,25);
}
</PerlCode><If: Field: SnippedSubject><Field: SnippedSubject>...<If: Else><Field: Subject></If><br />
<PerlCode>
{
 $SnippedSubject = "";
}
</PerlCode>


The following can freely be used:
Code: Select all
<Snip 25: Field: Subject><br />


Hmmmm, isn't that the standard snipping method?
I was looking for the abrupt character cutoff method.


As far as I know there are 2 classic snipping methods used in CMS

1) The classic word snip based on character count
Now is the time for all good men.
Now is the time for all...

2) The abrupt character snip, cutting off at a precise chr. no.
Now is the time for all good men.
Now is the time for all goo...

There are many variants derived from those models, with and
without trailing periods etc. but imo. they are all very imortant
to layouts.

In my Coranto news building guide ( in progress ) I have decided
to dedicate a section entirely to snipping complete with examples
and code. :D
I went deeper into the belly of Coranto and look! I found maginot.
User avatar
Esstee
 
Posts: 320
Joined: Thu Nov 25, 2004 4:13 pm
Location: Deep in some project

Postby LoneOwl » Fri Jun 03, 2005 12:23 pm

Using something like
Code: Select all
<PerlCode>my $Snipped = substr($Subject, 0, 25) . '...' if length $Subject > 25;</PerlCode>
<If: FIeld: Snipped><Field: Snipped><If: Else><Field: Subject></If>
would probably the the best method for doing it along a character boundary. <Snip: > would be best for a word boundary, and there are a few other methods. For paragraph boundaries, this would work well.
Code: Select all
<PerlCode>
my $Paragraphs;
do {
    my $delim = "<br><br>";
    my $pos;
    for (my $i = 0; $i < 3; $i++) {
        $pos = index $Text, $delim, $pos + 1;
        $pos == -1 and last;
    }
    $pos != -1 and $Paragraphs = substr $Text, 0, $pos;
};
</PerlCode>
<If: Field: Paragraphs><Field: Paragraphs><If: Else><Field: Text></If>
User avatar
LoneOwl
 
Posts: 1465
Joined: Sun Mar 10, 2002 2:57 am
Location: That one place, you know?


Return to Customization

Who is online

Users browsing this forum: No registered users and 1 guest

cron