Hi all,
I have a project I am working towards completing - wondering if anyone might assist:
Current Project - Populate an HTML Template
Need to do the following:
1 - Load Sample.pm & split into 3 parts
2 - Parts 1 & 3 go into tempfiles
3 - Middle part gets pushed into array @allow_edit
4 - @allow_edit gets split at = signs & then pushed into temnplate, loaded from template/editor.tmpl
Turns out that the error I was seeing was simply a placement problem with calling the Template - thanks for the help.
#!/usr/bin/perl
use HTML::Template
# Open File - Read File Contents Then Modify & save contents
# Specify name of file
$data_file="sample.pm";
# Name of temp file 1
$prefile1="/tmp/123456.beg";
# Name of temp file 2
$prefile2="/tmp/123456.end";
$action = 1;
# Open File abd read it all in to rawdata
open (file_beg, ">$prefile1") || die ("Could not open file. <br> $!");
+# Open The File
open (file_end, ">$prefile2") || die ("Could not open file. <br> $!");
+# Open The File
open (sample, "$data_file") || die ("Could not open file. <br> $!");#
+Open The File
flock(sample, 2) or die "cannot lock file exclusively: $!";# Lock The
+File
@rawdata = <sample>;# Put data from file into array called sample
# write data from sample.pm into beg_non_edit
foreach $value (@rawdata) {
print ("$value\n");
if($value =~ /# __END_CONFIG__/i) {
$action=3;
}
if ( $action == 1 ) {
# write to file_beg
print file_beg "$value"; # as long as action is 1 it should wr
+ite to this file.
}
if ($action == 3) {
# write to file_end
print file_end "$value"; # as long as action is 3 it will writ
+e to this file
}
if ($action == 2) {
push @allow_edit, $value; # copy string to new array
}
if($value =~ /# __START_CONFIG__/i) {
$action=2;
}
}
close (sample);
close (file_beg);
close (file_end);
$template = HTML::Template->new(filename => 'template/editor.tmpl');
my @loop;
#fill in the array @loop with hash references from @allow_edit...
foreach (@allow_edit){
my($name, $value) = split /=/,$_;
push @loop, {part1=>$name, part2=>$value};
}
# fill in the loop,
foreach my $name (sort keys %loop_data) {
# get the color and shape from the data hash
my ($name, $value) = @{$loop_data};
}
#call param to fill in the loop with the loop data by reference.
$template->param(loop => \@loop);
print "Content-Type: text/html\n\n";
# print the template
print $template->output;
<HTML>
<HEAD>
<TITLE>Test Template</TITLE>
</HEAD>
<BODY>
<FORM ACTION="/" METHOD="POST">
<INPUT TYPE=HIDDEN NAME="Module" VALUE="EDITOR">
<INPUT TYPE=HIDDEN NAME="Method" VALUE="set">
<INPUT TYPE=HIDDEN NAME="temp_name" VALUE="123456">
<INPUT TYPE=HIDDEN NAME="actual_name" VALUE="sample.pm">
<INPUT TYPE=HIDDEN NAME="rowCount" VALUE="<TMPL_VAR NAME="ROWC
+OUNT">">
<TABLE BORDER="2">
<!-- start of loop -->
<TMPL_LOOP NAME="loop">
<tr>
<td align="right"><TMPL_VAR NAME="part1"></td>
<td><INPUT TYPE=text NAME="<TMPL_VAR NAME="par
+t1">" VALUE="<TMPL_VAR NAME="part2">"></td>
</tr>
</TMPL_LOOP>
<!-- end of loop -->
</TABLE>
<INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="Submit">
</FORM>
</BODY>
</HTML>