Gossamer Forum
Home : General : Perl Programming :

Header & Footer

Quote Reply
Header & Footer
Hi

I am writing my first script and having a problem getting the header and footer to work:

I have created a vcariable:

$htmlfooter = "footer.htm";

Then used:

#######################################
open (FOOTERSOURCE, "$htmlfooter");
@footer=<FOOTERSOURCE>;

#######################################



then where i want to use the footer i am using:

print "@footer";

but that is printing the location of the file not the file itself?

Any suggestions?
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] Header & Footer In reply to
The way I would do it, is;

Code:
#!/usr/bin/perl

$template = "/path/to/file.html";

open(FILE, $template) || die "Unable to open file. Reason $!";
@template_got = <FILE>;
close(FILE);

foreach (@template_got) {
$header_template .= $_;
}

Hope that helps, and doesn't confuse you even more :p

BTW: Before Paul, or someone else jumps in, I know its probably not the best method, but it works Smile

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [katabd] Header & Footer In reply to
Quote:
open (FOOTERSOURCE, "$htmlfooter");
@footer=<FOOTERSOURCE>;

#######################################



then where i want to use the footer i am using:

print "@footer";[/code]
Hmm a couple of points, there is no error checking after open() so it may never open the file and you wouldn't know until you realised there was no output. Also you aren't closing the file handle, open file handles should always be closed.

print "@footer";

...is ok but putting the array inside quotes changes the output, which you may not be expecting, for example:

my @a = ('a','b','c');
print @a;

prints..... abc

but:

print "@a";

prints..... a b c

and:

local $" = '-';
print "@a";

prints..... a-b-c

Here's how I'd do it:

Code:
my $file = '/path/to/file.html';
my $string;

open INC, $file or die "Can't open $file: $!";
$string = do { local $/; <INC> };
close INC;

print "Content-type: text/html\n\n";
print $string;
Quote Reply
Re: [Andy] Header & Footer In reply to
Thanks

#!/usr/bin/perl

$template = "/path/to/file.html";

open(FILE, $template) || die "Unable to open file. Reason $!";
@template_got = <FILE>;
close(FILE);

foreach (@template_got) {
$header_template .= $_;
}




Then what should I use to print the file within the script ?

is it $header_template?

& paul:

For some reason your suggestion is returing : $string in the script output bot the content of the header file..
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] Header & Footer In reply to
Im not sure what you mean.
Quote Reply
Re: [Paul] Header & Footer In reply to
Paul I though if the file is not HTML (like text or another CGI called through SSI) I will have to use another approach..

Thanks
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [Andy] Header & Footer In reply to
Hi Yogi

I tried this:

In the variables I have:

$template = "footer.htm";

Right after the variable section i added:

open(FILE, $template) || die "Unable to open file. Reason $!";
@template_got = <FILE>;
close(FILE);

foreach (@template_got) {
$header_template .= $_;
}

Then in the code I used

print "@template_got";

and @template_got

and neither one worked they returned a blank page..

any suggestions?
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] Header & Footer In reply to
The example I provided above works fine so if you can show the code you are using the problem may become clearer, otherwise I'd have to say I don't know what is wrong.
Quote Reply
ReplyBot In reply to
I am yogi's reply-bot, trained to reply to Paul's messages!
Quote Reply
Re: [Paul] Header & Footer In reply to
Hi

Here is what i have tried:

1- created a variable:

# Define Variables #$mailprog = '/usr/sbin/sendmail -i -t';
my $file = 'footer.htm';
my $string;

2- Right after the variable i have added:

open INC, $file or die "Can't open $file: $!";
$string = do { local $/; <INC> };
close INC;

3- Within the cgi script i have the template parsed as followed:

print "Content-type: text/html\n\n";

<html><head><title>Error: </title>
</head>
<center><table border=0 width=600 bgcolor=#9C9C9C>
<tr><th><font size=+2>Error: Blank Fields</font></th></tr></table>
<table border=0 width=600 bgcolor=#CFCFCF>
<tr><td>The following fields were left blank in your submission form:<p><ul><font color=$headerfont>$missing_field_list</font></ul><br>

These fields must be filled in before you can successfully submit the form.<p></td></tr></table></center>

print $string;
</body></html>

4- in the actulal page i get everything except the last section it prints:

print ;

Any ideas?
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] Header & Footer In reply to
Because you are trying to print within a string....change:

print $string;

to

$string

But after your header you don't seem to be actually printing the html as you have...

<html>...bla....

It needs to be:

print qq|<html.....|;

Last edited by:

Paul: Sep 1, 2002, 8:38 AM
Quote Reply
Re: [Paul] Header & Footer In reply to
Hi paul

Thanks

here is the problem..

I was inserting the part:

open INC, $file or die "Can't open $file: $!";
$string = do { local $/; <INC> };
close INC;

on the top of the script after the variable section..

That part should be inserted in each Sub that generates HTML pages in order for it to work..

The question:

Is there a way to generate or write a new sub that will make those files (footer and header0 available in all the html generated by the script without going through the subs one by one..?!

Thanks again Paul for the help in patience Smile
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] Header & Footer In reply to
Well you could add:

use vars qw/$string/;

...to the top of your script, then you can use $string anywhere in the script if thats what you mean?