Gossamer Forum
Home : Products : Links 2.0 : Discussions :

Is there more than IF for template file syntax ?

Quote Reply
Is there more than IF for template file syntax ?
I was wondering if in the template files we can do more than just check for the existence of a field with IF.

I'd like to have at least ELSE. Reason is that I'm planning on setting up a database that does not contain a URL for all links, so I want to print a link with the title when there is a URL provided and just print the title without a link when there isn't one.

IF .. ELSE .. ENDIF would be the construct to do this with. Can this be done though ? Any workaround if it can't ? I mean I'd need to at least be able to negate a condition, ie. IF NOT(..).

Georg
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
  Georg,
You don't need the ELSE keyword to achieve what you want. Smile You can edit it directly in the site_html_templates.pl
If you're still confused with what i'm talking about. take a look at this url:
http://216.169.107.32/...um3/HTML/000687.html


----------
Joe Thong
http://elara.hypermart.net/
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
Well, I'd like it the elegant, not round about, way, if possible.

Alex, could you provide at least a simple IF THEN ELSE syntax for the templates files ?

This would simplify things a lot instead of tinkering with files like site_html_templates.pl.

Georg
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
Georg,

I agree. I also asked Alex to include this a few weeks back but he never replied so I am not sure he even saw it.

The whole concept of templates is to put the html code in the template file without having to do a lot of modification to the scripts. Without <%elsif%> or <%else%> tags, a lot of template functionality is lost and we are back to adding stuff in site_html_templates.pl. Error messages are directly proportional to the number of lines added to a script! Proof is right here in these forums. I would estimate that well over 50% of the problems reported in these forums with Links is from people mucking about in the scripts. Smile The less they have to do in them, the less errors there are.

Heck, I would even like to see an <%ifnot variable%> tag, if nothing else.

------------------
Bob Connors
bobsie@orphanage.com
www.orphanage.com/goodstuff/


[This message has been edited by Bobsie (edited February 21, 1999).]
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
Hi,

(I did read the message, sorry about not responding!)

I would like to expand the template support, however it will never become as advanced as the level of control with site_html.pl. I don't think you'll ever see loop controls or anything like that in it. However, both else and not, and nested if's are all great ideas!

Unfortunately, my priority at the moment is finishing the SQL version. I'll definately add it to the todo list though. =)

Cheers,

Alex
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
Thanks Alex, I was getting worried a little that my request got overlooked.

I'm not asking for the fancy stuff, don't worry. I just think that NOT and IF-THEN-ELSE are not just a good idea, but a necessity.

I'm wondering though, how hard it can be ? Apparently the core of it all is in Template.pm:

Code:
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;
s/$begin\s*(.+?)\s*$end/
if (exists $self->{'vars'}{$1}) {
ref ($self->{'vars'}{$1}) eq 'CODE' ? &{$self->{'vars'}{$1}} : $self->{'vars'}{$1};
}
else {
return "Unkown Tag: $1";
}
/goe;
$self->{'parsed'}{$template} .= $_ . "\n";
}
return $self->{'parsed'}{$template};

Unfortunately, my knowledge of Perl is not enough to sufficiently understand the concepts in the few quoted lines of code.

Couldn't you at least give a hint on how the NOT could be implemented ? It shouldn't be too hard. The IF-THEN-ELSE construct could then wait.

Georg

[This message has been edited by Georg (edited February 23, 1999).]
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
I added the <%ifnot variable%> to template.pm.
It works with the same constraints that <%if variable%>
has (ie, must end with <%endif%>, must be on own line).

I have tested it and have found no problems, but that
doesn't guarantee that it works right.

In parse of template.pm, you need to add the following
line (the first and last lines are already present):

/$begin\s*if\s*(.+)?\s*$end/o and ($self->{'vars'}{$1} !~ /^\s*$/) ? ($go = 1) : ($go = 0) and next;
/$begin\s*ifnot\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;

Hope this helps,
Cal
cal@calsweb.com

[This message has been edited by Cal (edited February 28, 1999).]

[This message has been edited by Cal (edited February 28, 1999).]
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
Gee, it's that easy ?

Thanks a lot. It works great.

Only thing left now in this department is to get (Active-) Perl to behave properly with the end-of-line characters when using the Web editing templates interface on a Windows 95 (Apache) server.

Right now it's inserting another 0x0D for each 0x0D it finds, which results in inserting a blank line with each editing increment of a template file. Very annoying.

Georg
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
Here's a mod to let you include files. Syntax is:

<%include filename.txt%>

and make sure filename.txt is in your templates directory. Inside your included file you can also include tags as well! The code below also includes the <%ifnot .. %> changes described by Cal.

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

# Parse includes.
s#$begin\s*include\s*(.+?)\s*$end#
if (exists $self->{'inc'}{$1}) { $self->{'inc'}{$1}; }
else {
open (INC, "${$self}{'ROOT'}/$1") or return "Can't find file: ${$self}{'ROOT'}/$1";
$self->{'inc'}{$1} = join ("", <INC> );
close INC;
$self->{'inc'}{$1};
}
#goe;

# Parse tags.
s/$begin\s*(.+?)\s*$end/
if (exists $self->{'vars'}{$1}) {
ref ($self->{'vars'}{$1}) eq 'CODE' ? &{$self->{'vars'}{$1}} : $self->{'vars'}{$1};
}
else {
"Unkown Tag: $1";
}
/goe;
$self->{'parsed'}{$template} .= $_ . "\n";
}

I'm still looking into the file editing problem.

Cheers,

Alex
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
I tried this, but the data is not added without the url.

====
make sure filename.txt is in your templates directory.
====
I have a blank text file in my template directory - is that what you mean?

Am I supposed to leave this at the bottom?
====
}
return $self->{'parsed'}{$template};
}

Thanks

[This message has been edited by socrates (edited February 28, 1999).]
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
Hi Socrates,

The changes above includes the contents of a file into your page. If the file is blank, then of course nothing will be added.

The syntax is <%include filename.txt%> which will include the contents of a file called filename.txt found in your templates directory.

Hope this helps,

Alex
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
With this new <%include filename.txt%> option, can I place it in a category field?

Such as in the header field could I put <%include file_for_this_cat.txt%> in the field?

Meaning I'd like to include a good chunk of code into each category header, that so far I have been having to put directly into the header...making the .db rather large.

Before you say just use the header option, this is special code for each category, no two are the same.
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
 
Quote:
Meaning I'd like to include a good chunk of code into each category header, that so far I have been having to
put directly into the header...making the .db rather large.

Before you say just use the header option, this is special code for each category, no two are the same.

You can put a filename in the header field and Links will include it. For instance, put a file called computers.txt in a directory called 'headers'. Then for the computers category, put 'computers.txt' into the header fields. Links will then open up the file and include it for you.

Cheers,

Alex
Quote Reply
Re: Is there more than IF for template file syntax ? In reply to
Hi Alex,

OK, I understand that this works like a template, just like other html templates. HOwever, as I mentioned above, when I input a link into the database, it will not accept unless I enter the "url" ie if the "url"field is left blank.

======
The syntax is <%include filename.txt%> which will include the contents of a file called
filename.txt found in your templates directory.
======

If it accepts and writes the info into the database then I can use the file.txt with <%include filename.txt%> just like I call other html templates.

Am I understanding this, could you please explain.