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

Global Tag to Shorten Description

Quote Reply
Global Tag to Shorten Description
Hi:

I looked around a bit- in ALL of the Links versions, too- to see if someone had done this, and I did not see anything.... but I thought I would ask and see if anyone had done this yet.

What I was thinking is that I have a lot of fairly long Descriptions- and that is fine. But what I would like, is to make a global tag- call it "short_desc" that would take Description, and if it is longer than XX characters, chop it off. Of course, ideally, it would chop at a white space BEFORE the XX Character limit.

Then, one could use <%short_desc%> on category pages, and the full description on the detailed.

Anyway, andone have anything like that right now, or some ideas where to start?

THANKS!

Dave

Quote Reply
Re: Global Tag to Shorten Description In reply to
Hi,

Sure, add a global tag called short_desc that looks like:

Code:
sub {
my $tags = shift;
my $desc = $tags->{Description} or return "No description on this template";
length $desc < 100 and return $desc;
my $short = substr ($desc, 0, 100);
$short =~ s/\s\S+?$//;
$short .= " ...";
return $short;
}
Then use <%short_desc%> on the template page you want. Hope that helps,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Global Tag to Shorten Description In reply to
Alex:

MAN that was quick! Thanks a TON!

Dave

Quote Reply
Re: Global Tag to Shorten Description In reply to
Alex:

Tried it, and it ALMOST worked... I added the tag to globals, called it "short_char" (I am REALLY using it for my own "Characters" tag, not Description...) and made the change to link.html...

but then every link on a page had the shortened tag, but they all had the SAME shortened tag.... That is, on the Disney Features page, the Snow White characters were shortened, but also copied to Fantasia, Pinocchio, etc...

dave

Quote Reply
Re: Global Tag to Shorten Description In reply to
OK, I THINK I know the problem.... but I definitely found an answer!

I think the global must just return one variable per page built, and thus I only got one short list of characters per page. So, I figured I needed to tweak the link where it is built... in SiteHTML.pm

So, I adjusted Alex's code to make it work in a script, and came up with:
In Reply To:
# Set Up a Short Character List

my $short;
my $desc = ${$rec}{'Characters'};
if (length $desc < 100) {
$short = $desc;
}
else {
$short = substr ($desc, 0, 100);
$short =~ s/\s\S+?$//;
$short .= "...";
}
$rec->{short} = $short;
And this worked right out of the box!

One question to the perl guru's, if I could... would you explain EXACTLY what

$short =~ s/\s\S+?$//;

Means? And, since I assume this is the part that cuts at a white space, and since I am using this in my characters list, can I mod it to cut just before the last comma before 100 characters?

(A character list might look like: "Captain Kirk, Mr. Spock, Dr. McCoy, Scotty, Lieutenant Sulu, Lieutenant Uhura, Transporter Chief Kyle, Pod Ship, Malignant alien." The above code chops this to: "Captain Kirk, Mr. Spock, Dr. McCoy, Scotty, Lieutenant Sulu, Lieutenant Uhura, Transporter Chief...." Which is OK... but I would want to cut it between "Uhura" and the following comma...)

One final note!~ The above code is to shorten Characters (which is my own field). If you use this for Description, make the approipriate edits!

dave



Quote Reply
Re: Global Tag to Shorten Description In reply to
I see the problem, and why the fix works.

I think the problem is not that it's building one page, but that it's compiling the sub only once, rather than for each sub-template. Perhaps the answer is to put a reference to the subroutine, rather than a subroutine itself. That would cause the the routine to be reevaluated each time it's referenced, rather than just the first time.

Alex will have to address this specifically, but either way, it seems to be a logical/programmatic bug that should be addressed and either fixed, or worked around since it's not behaving as "expected".



PUGDOG«
PUGDOG« Enterprises, Inc.
FAQ: http://pugdog.com/FAQ


Quote Reply
Re: Global Tag to Shorten Description In reply to
Pugdog:

From the results I got, I just figured that the "global" was just run once per page, rather than evaluated for each result per page... maybe that is the way globals are? After all, the page title does not change going down the results...

I guess what I am sayiong is maybe the gl;obal is acting correctly, but just not the approriate place to do this... maybe as a plug in? (I have not even gotten to those. Well, I have, but it hurts my head...) Smile

dave

Quote Reply
Re: Global Tag to Shorten Description In reply to
What I'm saying, is, that on the category page, where it calls the sub-templates "link.html" and perhaps others, that the "globals.txt" file needs to be evaluated each time -- rather than once for the 'category.html' page.

It seems Alex felt (or inferred) that that is how it would behave. This seems to be unexpected, or un-planned behavior and needs to be "fixed" one way or another (either as feature or bug) in the upcoming release.

Either way, it would need it's own explanation or description in the docs.

But, it's something Alex needs to address directly.

PUGDOG«
PUGDOG« Enterprises, Inc.
FAQ: http://pugdog.com/FAQ


Quote Reply
Re: Global Tag to Shorten Description In reply to
Hi,

I just tested this locally and it ran as expected, a different description for each link. I can't see how it would return the same one, very strange.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: Global Tag to Shorten Description In reply to
I found a neat way to use Alex's sub-routine. I wanted the category pages to have a short description, but if people did a search the link needed to show the entire description in order to show the person where the word was found within the long description.

All I did was add the statement to the 'links.html' template and it works great:

<%if query%><%Description%><%else%><%short_desc%>
<%endif%>

Hope that helps someone that might be trying to do the same thing. I didn't realize that 'query' could be used in the templates, until I tried it out.

Bryan

Quote Reply
Re: [BryanL] Global Tag to Shorten Description In reply to
what does 'query' then do?

What does the tag mean? :-)

In Reply To:
I found a neat way to use Alex's sub-routine. I wanted the category pages to have a short description, but if people did a search the link needed to show the entire description in order to show the person where the word was found within the long description.

All I did was add the statement to the 'links.html' template and it works great:

<%if query%><%Description%><%else%><%short_desc%>
<%endif%>

Hope that helps someone that might be trying to do the same thing. I didn't realize that 'query' could be used in the templates, until I tried it out.

Bryan
Quote Reply
Re: [bannerzone] Global Tag to Shorten Description In reply to
Query is generated from the search.cgi. When you do a search.

Also, do a search here a few months back, you'll find a thread on highlighting search terms. i worked on a global for someone, and then extended it to allow toggling the description. The fixed/completed global was posted, with a few things I learned about trying to use 'query' in sub-templates.


PUGDOG´┐Ż Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Global Tag to Shorten Description In reply to
Thank you :)

I will take a look at it ...

Bent