Gossamer Forum
Home : Products : Links 2.0 : Customization :

How to create an "if categories not blank" statement.

Quote Reply
How to create an "if categories not blank" statement.
I would like to add a horizontal line to separate the sub-categories, from the links that are in a category. I am using templates, and tried adding one in category.html in the appropriate place which worked, except I run into a problem when there is related categories. I only want to include it when there are categories or related categories, not when there are just links.

I presume I need an if statement, but not being familiar with perl or cgi, I don't know the actual code I should use. What I think I am looking for is something like

<%if category%>
<%if related%>
<hr noshade color="#008000">
<%endif%>
<%endif%>

Would this work? Any help would be appreciated.

[This message has been edited by lunaria (edited February 17, 1999).]
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
Sadly, as I found out, you cannot nest <%if tagname%> links_tags.

------------------
Bob Connors
bobsie@orphanage.com
www.orphanage.com/goodstuff/
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
  You're facing the similar problem which i encountered few days ago. After studying Links2 for a while, i discovered that the results which i wanted can be done by editing the site_html_templates.pl

First of all, open up site_html_templates.pl and make the following changes to sub site_html_category


sub site_html_category {
# --------------------------------------------------------
# This rountine will build a page based for the current category.
if ($related) {
$relatedYesNoCode = '<hr noshade color="#008000">';
}
else {
$relatedYesNoCode = '';
}
return &load_template ( 'category.html', {
date => $date,
time => $time,
category => $category,
links => $links,
title_linked => $title_linked,
title => $title,
total => $total,
grand_total => $grand_total,
category_name => $category_name,
category_name_escaped => $category_name_escaped,
category_clean => $category_clean,
description => $description,
meta_name => $meta_name,
meta_keywords => $meta_keywords,
header => $header,
footer => $footer,
prev => $prev,
next => $next,
related => $related,
build_links_per_page => $build_links_per_page,
relatedYesNoCode => $relatedYesNo;
%globals
} );
}

--------------------------------------------

Then open up category.html and type it like this:

<%if category%>
<%relatedYesNoCode%>
<%endif%>

--------------------------------------------


If there's no related categories, it won't print anything, if there is it'll print the <hr> tag.

Hope this works. I haven't tried this on my server :-), but this is what i did to my Links2 when there isn't an <%else%> links_tag.


------------
Joe Thong
http://elara.hypermart.net/
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
I used your suggestion with a few modifications (and some typo corrections) and it worked beautifully!

This is what I did.
In site_html_templates.pl
Quote:
sub site_html_category {
# --------------------------------------------------------
# This routine will build a page based for the current category.
if ($category) {
$lineYesNoCode = '<hr noshade color="#000000" size="2">';
}
elsif ($related) {
$lineYesNoCode = '<hr noshade color="#000000" size="2">';
}
else {
$lineYesNoCode = '';
}

This sets lineYesNoCode to write a line if there is either a category or a related category, otherwise it sets it as blank.

And further down I added

lineYesNoCode => $lineYesNoCode,

In category.html, after the categories and related categories i put this.

<%lineYesNoCode%>

Now it puts a horizontal line between the categories or related categories and the links listings.

Thank you sooo much!

P.S. I've modified this post, as what I had before was incorrect.

[This message has been edited by lunaria (edited February 23, 1999).]
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
Lunaria,
Oops, now only i realized that i have a typo in the code. Smile Glad you figured it out yourself.
Cheers

---------
Joe Thong
http://elara.hypermart.net/
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
This is what I did in the templates (category.html). The line is displayed only when there are subcategories at the top of the page since no categories and hence the line is not printed, if there are none. Did a similar setup at the bottom for "related categories"

Am I missing something?

<!-- Subcategories-->
<%if category%>
<font face="arial"><h4>Categories:</h4>
<%category%></font>
<hr size="0">
<%endif%>
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
If you would like to make a few changes to parse
in Template.pm, you can have nested ifs.

The following lines:
Code:
my $go = 1;

foreach (@lines) {
/$begin\s*if\s*(.+)?\s*$end/o and ($self->{'vars'}{$1} !~ /^\s*$/) ? ($go = 1) : ($go = 0) and next;
/$begin\s*endif\s*$end/o and ($go = 1) and next;
$go or next;

need changed to:

Code:
my @go = (1,1);
my $depth = 1;

foreach (@lines) {
/$begin\s*if\s*(.+)?\s*$end/o and ($self->{'vars'}{$1} !~ /^\s*$/) ? ($go[++$depth] = $go[$depth]) : ($go[++$depth] = 0) and next;
/$begin\s*ifnot\s*(.+)?\s*$end/o and ($self->{'vars'}{$1} !~ /^\s*$/) ? ($go[++$depth] = 0) : ($go[++$depth] = $go[$depth]) and next;
/$begin\s*endif\s*$end/o and ($go[$depth--] = 1) and next;
$go[$depth] or next;

This also includes the <%ifnot...%> code change.
I have tested this and found no problems. You can
find the changes at http://raburn.net/mods/nested.html , also.

Good Luck,
Cal
cal@calsweb.com

[This message has been edited by Cal (edited March 12, 1999).]
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
Cal,

Could you please give an example of how might be able to use this if not statement?

How are you using this for your site?

Thanks
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
Socrates,

<%ifnot...%> works just like <%if..%> except
<%if variable%> tests to see if there is the 'variable'.
<%ifnot variable%> test to see if the variable
doesn't exist. <%ifnot ...%> must end with a <%endif%>
like <%if...%> does.

Possible example:

<%if test%>
This part is used if the variable 'test' is present.
<%endif%>
<%ifnot test%>
This part is used if the variable 'test' is not present.
<%endif%>

As for how I use it, I have set up links and have it
running correctly, but I haven't edited the templates
and it looks just like it does when you download
it. (I really need to work on it.)

I do have a question of my own. What happens if the
variable you test for is set to ' ' or '' ?

Cal
cal@calsweb.com
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
I do have a question of my own. What happens if the variable you test for is set to ' ' or '' ?

Thanks Cal, for your reply. I am also wondering about that. For example I have a detailed view for most links. Some may not have details.

so I could use
<%if isDetailed%>
<%Detailed%>
<%endif%>
<if notDetailed%>
NOW WHAT?
<%endif%>

By the way is it not necessary to have a 'Yes,No' already defined in the links.def if you want to use both if and endif?
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
socrates,

Quote:
so I could use
<%if isDetailed%>
<%Detailed%>
<%endif%>
<if notDetailed%>
NOW WHAT?
<%endif%>

That is incorrect. You would need to use:

Quote:
so I could use
<%if isDetailed%>
<%Detailed%>
<%endif%>
<%ifnot isDetailed%>
NOW WHAT?
<%endif%>

As to the "NOW WHAT" part of that, if you have nothing to put there, you wouldn't even use the <%ifnot isDetailed%> .. <%endif%> in the first place, so the question doesn't even need to be asked.

Quote:
By the way is it not necessary to have a 'Yes,No' already defined in the links.def if you want to use both if and endif?

Yes, isDetailed would have to have a Yes/No field but not to use the <%if isDetailed%> and <%ifnot isDetailed%> tags. Those two tags don't check to see what the value in isDetailed is. They just check to see if isDetailed is null or not. <%if isDetailed%> would return TRUE if either Yes or No are contained in it. It would return FALSE if nothing is contained in it. The opposite case exists for <%ifnot isDetailed%>. It will return TRUE if isDetailed is empty and FALSE if something is in it (i.e., Yes or No).

The place to check to see what is in isDetailed would be in site_html_templates.pl before the template is called.

I hope this is clear. If not, ask away!
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
Hi,

I think you are making it more difficult then it has to be. First off, just to clairify, <%if variable%> will print everything after it up to the <%endif%> tag if variable is true. True in perl means for a string that the variable is defined and is not an empty string. So '' and "" are empty strings and considered false, and if the variable doesn't even exist it will also be false.

Now, for the Detailed tag. Why not just:

<%if Detailed%>
<%Detailed%>
<%endif%>

which says if the link has something in the Detailed field, print it out, otherwise don't. Of course if that's all you where doing, you could just do:

<%Detailed%>

as if Detailed is empty, you just get an empty string. The only reason for the if tags is if you wanted to do:

<%if Detailed%>
Here is the detailed view:
<%Detailed%>
<%endif%>

i.e., display some text only if Detailed has some content to it.

Hope this helps,

Alex
Quote Reply
Re: How to create an "if categories not blank" statement. In reply to
Alex,

I will have to try that again. I had tried that even before I got into this conversation. I had added an extra field for "review" and I typed in a review for one of the links.

But I just saw the review being printed where there was one and the rest of them printed <%Review%>.

By the way, is it possible to have the "review or detailed" files in their corresponding directories (for each category) rather than having them all in one directory? All of my sites will have a detailed view. I am afraid this directory will be huge.

Please comment.
Quote Reply
Re: [socrates] How to create an "if categories not blank" statement. In reply to
I'm new to links2 so take it easy on me here.... I've researched the entire forum on the use of <%if related%>
do something
<%endif%>
<%ifnot related%>
do something else
<%endif%> BUT, the <%ifnot... just does not work. How hard would it be to modify the script to make the <%ifnot...... work properly? Thanks
-----------------------------------------------------------
Professional Search Engine Marketing by Position Concepts Professional Consulting in SEM, SEO and PPC.