Gossamer Forum
Home : Products : Gossamer Links : Discussions :

If Question

Quote Reply
If Question
I have two different size margin banners, one long and one short. I want to display the long one on the links page whenever possible, but when there are only a few links the long banner creates too much blank white space. Is there an “if” statement or global that I could use for determining the link count on a page and display the corresponding banner?
Such as %if link% less than 5 display short banner %else% display long banner? Seems as though there is a way to do I just do not know how, or the correct syntax.
Thanks-

Quote Reply
Re: [jgkiefer] If Question In reply to
Lets, see... there's links_loop which is an array that contains all the links on that page. If you get the number of elements in the array, you'll get the number of links on that page. You can do this by adding an entry to your globals.txt
Code:
'link_count' => 'sub {
my $vars = shift;
if ($vars->{links_loop}) {
return @{$vars->{links_loop}};
}
return 0;
}',
Then in your templates (eg. category.html), you can use:
Code:
<%if $link_count > 10%>
There are more than 10 links!
<%else%>
There are less than 10 links.
<%endif%>
Hope this helps.


Adrian
Quote Reply
Re: [brewt] If Question In reply to
That's so easy!
I swear you guys (gals) at GT are the smartest people I've ever encountered.
Blush (Gee, nice and smart too, what a combo.)
Thanks-


Quote Reply
Re: [brewt] If Question In reply to
What do I name this entry in the globals?
Link count?

Quote Reply
Re: [brewt] If Question In reply to
return @{$vars->{links_loop}};

Wouldn't that return the links rather than the total?

I know you are probably right but I thought it may be...

return scalar @{$vars->{links_loop}};
Quote Reply
Re: [jgkiefer] If Question In reply to
In Reply To:
What do I name this entry in the globals?
Link count?
If you do it from the admin you would make the name link_count (or whatever you want to name it).

and the contents in the textbox field would be:
Code:
sub {
my $vars = shift;
if ($vars->{links_loop}) {
return @{$vars->{links_loop}};
}
return 0;
}

Adrian
Quote Reply
Re: [RedRum] If Question In reply to
In Reply To:
return @{$vars->{links_loop}};

Wouldn't that return the links rather than the total?

I know you are probably right but I thought it may be...

return scalar @{$vars->{links_loop}};

Both work Smile. If it was just $vars->{links_loop} it'd pass the array, but passing it as @{$vars->{links_loop}}, is pretty much using it in a scalar context. I suppose putting the 'scalar' in makes it a little more explicit.

Adrian
Quote Reply
Re: [brewt] If Question In reply to
Thanks, that makes it perfectly clear... I think.
Tongue

Quote Reply
Re: [brewt] If Question In reply to
I gave it a go, but... using

Code:
return scalar @{$vars->{links_loop}};
does not seem to work, only displays banner_long all the time.

BUT Using -

Code:
sub {
my $vars = shift;
if ($vars->{links_loop}) {
return @{$vars->{links_loop}};
}return 0;
}


With -

Code:
<%if $link_count > 10%>
<%banner_long%>
<%else%>
<%banner_short%>
<%endif%>


Works almost, except that it displays banner_long when links are present, and banner_short when there are no links just categories.
Hummm... I must have screwed something up somewhere?


Quote Reply
Re: [jgkiefer] If Question In reply to
Oh sorry, but that remark about 'return scalar ...' was directed at RedRum. You'll need all that code to work.

If you want to comphensate for categories as well, you can return the number of categories + the number of links... then you could do something like:
Code:
'link_cat_count' => 'sub {
my $vars = shift;
return int(scalar(@{$vars->{links_loop}}) + scalar(@{$vars->{category_loop}}) / $Links::CFG->{build_category_columns});
}',

That calculates the number of links on that page, plus the number of categories / number of columns. Of course you can change this to whatever you want.


Adrian
Quote Reply
Re: [brewt] If Question In reply to
I can't seem to get it to work. I refuse to give up on this till I get it.
Okay, I created a template global called-

link_cat_count

then I copied into the global box-

'link_cat_count' => 'sub {
my $vars = shift;
return int(scalar(@{$vars->{links_loop}}) + scalar(@{$vars->{category_loop}}) / $Links::CFG->{build_category_columns});
}',

Next I put this if statement into the category.html page-

<%if $link_cat_count > 10%>
<%banner_long%>
<%else%>
<%banner_short%>
<%endif%>

maybe I should put the two banners in includes instead of making them globals? Shouldn't think it would matter. I have had some trouble when I copy the code from the forum page, so I may not have the syntax correct. Anyway I sure would appreciate you looking over my code to find the mistakes. I'm lost...
Frown

Quote Reply
Re: [jgkiefer] If Question In reply to
No, you only put:
Code:
sub {
my $vars = shift;
return int(scalar(@{$vars->{links_loop}}) + scalar(@{$vars->{category_loop}}) / $Links::CFG->{build_category_columns});
}
in the text box... You would only use the whole thing if you were directly editing the globals.txt file.


Adrian