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

Combination of two globals?

Quote Reply
Combination of two globals?
Hi I have these two globals below and I need a combination of these two.

The result of the combination should be a short description with line breaks

Global for short description
Code:
sub {
my $id = $_[0];
my $desc = $_[1];
my $chars = $_[2] || '300';
my $build_detail_url = $CFG->{build_detail_url};
my $build_extension = $CFG->{build_extension};

if (!$desc) { return "No description on this template"; }

length $desc < $chars and return $desc;
my $short = substr ($desc, 0, $chars);
$short =~ s/\s\S+?$//;

my $detailed_url = "$build_detail_url/$id$build_extension";
$short .= qq| ... <a href="$detailed_url">weiter lesen</a>|;
return $short;
}

Global for the line breaks
Code:
sub {
my $tags = shift;
my $Description = $tags->{Description};
$Description = GT::CGI::html_escape($Description);
$Description =~ s/\n/<br>\n/g;
return $Description;
}

Thanks
Matthias

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combination of two globals? In reply to
Hi,

one solution is to modify the first global:

Code:
sub {
my $id = $_[0];
my $desc = $_[1];
my $chars = $_[2] || '300';
my $escape = $_[3] || 0;
my $build_detail_url = $CFG->{build_detail_url};
my $build_extension = $CFG->{build_extension};

if (!$desc) { return "No description on this template"; }

if ( length $desc < $chars ) {
if ( $escape ) {
$desc = GT::CGI::html_escape($desc);
$desc =~ s/\n/<br />\n/g;
}
return $desc;
}
my $short = substr ($desc, 0, $chars);
$short =~ s/\s\S+?$//;

if ( $escape ) {
$short = GT::CGI::html_escape($short);
$short =~ s/\n/<br />\n/g;
}

my $detailed_url = "$build_detail_url/$id$build_extension";
$short .= qq| ... <a href="$detailed_url">weiter lesen</a>|;
return $short;
}

Regards

Merten
Quote Reply
Re: [neves] Combination of two globals? In reply to
 
Hi neves,
thanks for your help, but instead of an output, I get the following error :-(

Code:
Unable to compile 'description_short': Bareword "n" not allowed while "strict subs" in use at (eval 79) line 14. Bareword "g" not allowed while "strict subs" in use at (eval 79) line 14. Bareword "n" not allowed while "strict subs" in use at (eval 79) line 23. Bareword "g" not allowed while "strict subs" in use at (eval 79) line 23.

Do you know how to fix it?

Matthias

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combination of two globals? In reply to
Oops,

Code:
s/\n/<br />\n/g;

should be

Code:
s/\n/<br \/>\n/g;

Regards

Merten
Quote Reply
Re: [neves] Combination of two globals? In reply to
Hi neves,
now the error is gone but there are no line breaks anymore :-(

Below there is the whole global

Code:
sub {
my $id = $_[0];
my $desc = $_[1];
my $chars = $_[2] || '300';
my $escape = $_[3] || 0;
my $build_detail_url = $CFG->{build_detail_url};
my $build_extension = $CFG->{build_extension};

if (!$desc) { return "No description on this template"; }

if ( length $desc < $chars ) {
if ( $escape ) {
$desc = GT::CGI::html_escape($desc);
$desc =~ s/\n/<br \/>\n/g;
}
return $desc;
}
my $short = substr ($desc, 0, $chars);
$short =~ s/\s\S+?$//;

if ( $escape ) {
$short = GT::CGI::html_escape($short);
$short =~ s/\n/<br \/>\n/g;
}

my $detailed_url = "$build_detail_url/$id$build_extension";
$short .= qq| ... <a href="$detailed_url">weiter lesen</a>|;
return $short;
}

Matthias

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combination of two globals? In reply to
Hi,

do you need the line breaks as an option? If so, you need to pass all parameters to the global: <%description_short( $ID, $Description, 300, 1)%>

If not, delete the $escape-stuff:

Code:
sub {
my $id = $_[0];
my $desc = $_[1];
my $chars = $_[2] || '300';
my $build_detail_url = $CFG->{build_detail_url};
my $build_extension = $CFG->{build_extension};

if (!$desc) { return "No description on this template"; }

if ( length $desc < $chars ) {
$desc = GT::CGI::html_escape($desc);
$desc =~ s/\n/<br \/>\n/g;
return $desc;
}
my $short = substr ($desc, 0, $chars);
$short =~ s/\s\S+?$//;

$short = GT::CGI::html_escape($short);
$short =~ s/\n/<br \/>\n/g;

my $detailed_url = "$build_detail_url/$id$build_extension";
$short .= qq| ... <a href="$detailed_url">weiter lesen</a>|;
return $short;
}

Regards

Merten
Quote Reply
Re: [neves] Combination of two globals? In reply to
Thank you neves
now your global works fine :-)

First I called the global with
<%description_short( $ID, $Description, 300)%>

With your suggestion the global works fine
<%description_short( $ID, $Description, 300, 1)%>

One last question
Do I need the 300 in the global an in the template as well?
my $chars = $_[2] || '300';
<%description_short( $ID, $Description, 300,1)%>

Thanks
Matthias

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combination of two globals? In reply to
In Reply To:
Do I need the 300 in the global an in the template as well?
my $chars = $_[2] || '300';
<%description_short( $ID, $Description, 300,1)%>

Yes, because $_[0] is the first parameter $_[1] the second and so on.

If the 300 in your template is missing, $chars will become 1.

The 300 in your global is the default, when you call it with <%description_short( $ID, $Description)%>.

Regards

Merten
Quote Reply
Re: [neves] Combination of two globals? In reply to
Thanks for your help

Vielen Dank für die Zeit, die du dir genommen hast.
Wie kommts, dass du erst 9 Beiträge in diesem Forum hast, aber dich so gut auskennst?
Matthias

Matthias
gpaed.de
Quote Reply
Re: [Matthias70] Combination of two globals? In reply to
Weil ich das schon 'ne Weile mache, dem Forum aber nie so wirklich Beachtung geschenkt habe.
Außerdem bin ich schüchtern.Wink