Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HTML::Template problems 2

Status
Not open for further replies.

MJAZ

Programmer
Joined
Aug 1, 2006
Messages
73
Location
US
I can't seem to get html::template to do what i want it to do.
My program s supposed to read some .txt files and output the content (which is HTML fragments) onto the pages. I get a huge amount of errors. here is my code:

sub print_index {

my $maincontent_file_index = $ENV{DOCUMENT_ROOT}."/templates/content/index/maincontent.txt";
my $footercontent_index = $ENV{DOCUMENT_ROOT}."/templates/content/index/indexfootercontent.txt";
my $dropdown = $ENV{DOCUMENT_ROOT}."/templates/content/dropmenu.txt";
my $index_tmpl = $ENV{DOCUMENT_ROOT}."/templates/index.tmpl";
my @data1;
my @data2;
my @data3;
my $tmpl = new HTML::Template( filename => $index_tmpl );

my @content = [ content => @data1 ];
my @footer = [ footer => @data2 ];
my @dropdown = [ dropdown => @data3 ];

open(FILE,$maincontent_file_index) || die ("Could not open file:" .$!);
@data1 = <FILE>;
close (FILE);

open(FILE,$footercontent_index) || die ("Could not open file:" .$!);
@data2 = <FILE>;
close (FILE);

open(FILE,$dropdown) || die ("Could not open file:" .$!);
@data3 = <FILE>;
close (FILE);

$tmpl->param ( content => @content,
footer => @footer,
dropmenu => @dropdown );

print "Content-type: text/html\n\n";
print $tmpl->output;
exit;
}
 
here is my entire script:

#!C:/Perl/bin/perl.exe -wT

use CGI;
use HTML::Template;
use strict;

my $query = new CGI;
my $content = $query->param( "content" );

$ENV{DOCUMENT_ROOT} = "C:/Program Files/Apache Software Foundation/Apache2.2";

my $template_file = $ENV{DOCUMENT_ROOT}."/templates/main.tmpl";


unless ( $content ) {
print_index();
}

sub print_index {

my $maincontent_file_index = $ENV{DOCUMENT_ROOT}."/templates/content/index/maincontent.txt";
my $footercontent_index = $ENV{DOCUMENT_ROOT}."/templates/content/index/indexfootercontent.txt";
my $dropdown = $ENV{DOCUMENT_ROOT}."/templates/content/dropmenu.txt";
my $index_tmpl = $ENV{DOCUMENT_ROOT}."/templates/index.tmpl";
my @data1;
my @data2;
my @data3;
my $tmpl = new HTML::Template( filename => $index_tmpl );

my @content = [ content => @data1 ];
my @footer = [ footer => @data2 ];
my @dropdown = [ dropdown => @data3 ];

open(FILE,$maincontent_file_index) || die ("Could not open file:" .$!);
@data1 = <FILE>;
close (FILE);

open(FILE,$footercontent_index) || die ("Could not open file:" .$!);
@data2 = <FILE>;
close (FILE);

open(FILE,$dropdown) || die ("Could not open file:" .$!);
@data3 = <FILE>;
close (FILE);

$tmpl->param ( content => @content,
footer => @footer,
dropmenu => @dropdown );

print "Content-type: text/html\n\n";
print $tmpl->output;
exit;
}
 
Hi MJAZ,

What are the errors you're seeing?

Mike

I am not inscrutable. [orientalbow]

Want great answers to your Tek-Tips questions? Have a look at faq219-2884

 
You can't set single parameters like this:
Code:
        $tmpl->param ( content => @content,
                       footer => @footer,
                       dropmenu => @dropdown );

Using arrays in that way will not do what you want. For example, if you have three lines in the file you read into @content (say each line has a number and a newline on it), that line translates to:
Code:
        $tmpl->param ( content => "1\n",
                       "2\n" => "3\n",
                       footer => @footer,
                       dropmenu => @dropdown );

To set the parameters like that, you have to slurp the files into scalars, not use arrays.
 
Thanks guys.
 
The errors i get are

[Wed Aug 2 13:27:23 2006] index.cgi: HTML::Template : Attempt to set nonexistent parameter ' <ul id="nav">
[Wed Aug 2 13:27:23 2006] index.cgi: ' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params => 1) at C:\PROGRA~1\APACHE~2\Apache2.2\htdocs\index.cgi line 47
Content-type: text/html

that is from the second line of the drop-down menu html fragment i use.
 
How would I slurp the file into a scalar?
 
Scratch that I got it! Thanks for all the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top