Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

create summary of description

Quote Reply
create summary of description
Is there a quick way to make a plug-in or global to create a summary or "snippet" of the link description to display on the links page so the user has an idea what he will get if he clicks on the detail page? I would like to display the first 25 words or so.

I saw a post on here about creating a "snippet" but is used a mod to a templatE. I just need a global variable and I couldn't get any mod of that code to work as a global

thanks
Quote Reply
Re: [JonathanKincaid] create summary of description In reply to
Hi,

To do that via "words", you would probably need something like;

Code:
sub {

my $msg = $_[0];
my $max = $_[1];
my @cuts = split / /, $msg;

if ($#cuts <= $max) { return $msg; }

my $back;
for (my $i = 0; $i < $max; $i++) {
$back .= " ". $cuts[$i];
}
return $back;
}

Call it with: <%global_name($Description,25)%> in link.html Smile (where "25" is the number of words to show...)

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] create summary of description In reply to
Thanks! That works. I added in one line to add the "..." at the end

sub {

my $msg = $_[0];
my $max = $_[1];
my @cuts = split / /, $msg;

if ($#cuts <= $max) { return $msg; }

my $back;
for (my $i = 0; $i < $max; $i++) {
$back .= " ". $cuts[$i];
}
$back .= "...";

return $back;
}
Quote Reply
Re: [JonathanKincaid] create summary of description In reply to
Another approach is to measure by characters. I find this useful since 25 words could mean a lot of difference when it comes to page layout. This global will return a substring of whole words not greater than the allowed maximum of characters in length. It is called with <%global_name($Description, 125)%>, too.

Code:
sub {
my ($string, $length) = @_;
return undef unless defined $string;
return $string unless defined $length;

if (length $string <= $length) {
return $string;
}
else {
while (substr($string, $length - 1, 1) ne ' ') {
$length--;
next if $length < 1;
}
return substr($string, 0, $length - 1) . '...';
}
}
Quote Reply
Re: [Volker] create summary of description In reply to
Hi volker -

Thanks for this second solution. Your are right, sometimes 25 words is one line and sometimes it is two lines. I will try your solution too. I didn't want to break a word, but maybe that will be the best way.

I need to learn more perl!
Quote Reply
Re: [JonathanKincaid] create summary of description In reply to
We use following to truncate just about anything that we may like to (credit afinlr)


display_truncate ==>

sub {
my $tag = shift;
my $length = shift;
length $tag < $length and return $tag;
my $short = substr ($tag, 0, $length);
$short =~ s/\s\S+?$//;
$short .= " ...";
return $short;
}

Then in template

<%display_truncate($Description, 'XX')%>

where xx could be replaced by a number (of characters)

it could be used for anything .... long titles, long description etc...



Thanks
HyTC
==================================
Mail Me If Contacting Privately Is That Necessary.
==================================