Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Populating an HTML Template

by hmag (Acolyte)
on Mar 27, 2006 at 20:31 UTC ( #539526=perlquestion: print w/replies, xml ) Need Help??

hmag has asked for the wisdom of the Perl Monks concerning the following question:

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>

Replies are listed 'Best First'.
Re: Populating an HTML Template
by injunjoel (Priest) on Mar 28, 2006 at 00:15 UTC
    Greetings,
    Though there are many things that could be suggested about your code the most important is to adopt the use of strict and warnings.
    Other than that I get the feeling you have not provided all of the relevant code in the example you give.

    For instance where do %loop_data and $loop_data get initialized? What is suppose to happen to ($name,$value) in your
    foreach $line (@allow_edit){ .... }
    block?
    I think I follow what you are attempting up until you close your filehandles... Then things get murky.

    Empirically @loop will never hold anything since you never fill it. Your #fill in the loop, foreach block creates and fills in $name and $value that fall out of scope with each iteration, @loop is never involved.
    Now if I follow your intent, and this is a big stretch to say the least, here is my suggestion for what I think you are shooting for (However not having seen your HTML template its a guess at best).

    Untested mind you.
    my @loop; #fill in the array @loop with hash references from @allow_edit... foreach (@allow_edit){ my($name, $value) = split /=/,$_; push @loop, {$name=>$value}; }
    Then continue with your template calls.
    It would be helpful to see what the markup (HTML) looks like that you are filling with "loop" in your $template->param call.
    without that everything is simply a shot in the dark.


    Update
    Okay, thats definitely helpful.
    So given the code.
    <!-- 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 -->
    I am lead to think you would like $name to appear in "part1" and $value to appear in "part2". Am I getting warmer?
    Lets modify the loop I suggested earlier to accomplish this.
    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}; }
    That should do it. I hope that helps.
    And one more thing, have a look at the range operators for another way to handle your file partitioning.

    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo
      Hi again, Thanks all who replied - I did just update the SoPW with the Template code after the Perl - hope this helps.
      Hi again, Many thanks - I get an undefined parametr now, but will check it out - thanks again.
Re: Populating an HTML Template
by InfiniteSilence (Curate) on Mar 28, 2006 at 00:35 UTC
    On a related note, I cannot believe that you really want to create temporary files every time this is called. BTW, TMTOWTDI:
    local $/; open(IN,qq|adatafile.txt|) or die $!; my $infile = <IN>; my $begin_stuff; my $end_stuff; my %mid_stuff; close(IN); if($infile=~m/(.*)\#\s+__START_CONFIG__/s){ $begin_stuff = $1; } if($infile=~m/__START_CONFIG__(.*)\# __END_CONFIG__/s){ my $tmp = $1; while($tmp=~m/([^=\s]+)=([^=]+)\n/g){ $mid_stuff{$1} = $2; } } #if ..oh hell, you get the bloody idea... print Dumper \%mid_stuff;

    Celebrate Intellectual Diversity

Re: Populating an HTML Template
by samtregar (Abbot) on Mar 27, 2006 at 21:38 UTC
    I'm not sure what you're asking for here. Your task description doesn't make any sense to me, but the code you posted looks reasonable enough. Does it work? If not, can you describe the problem you're encountering?

    -sam

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://539526]
Approved by kutsu
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2020-10-21 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (212 votes). Check out past polls.

    Notices?