HTML::Templateの多重ループ

戻る

m_start.pl

use strict; use HTML::Template; use HTML::Template::Compiled; my (%t,@loop,$n,$n1,$ref,@members,%list); $t{N1select} = 2; $t{select} = 4; # ループデータ生成 @loop = (); for $n ( 1 .. 10 ) { $ref = (); $$ref{NO} = $n; ###################################################### # 第二层LOOP的做成,SELECTED表示该项目是初始值 # for $n1 ( 1 .. 5 ) { $t{N1} = 'N1' . $n1; if ( $n == $t{N1select} && $n1 == $t{select}) { $t{N2} = 'SELECTED VALUE=' . $n1; } else { $t{N2} = 'VALUE=' . $n1; } $list{$t{N1}} = $t{N2}; } @members = (); push @members, { %list }; $ref->{LOOP2} = [ @members ]; ####################################################### # 第二层LOOP的做成的原始方法 # $row{LOOP2} = [ # { # N11 => 'VALUE=1', # N12 => 'VALUE=2', # N13 => 'VALUE=3', # N14 => 'VALUE=4', # N15 => 'VALUE=5', # }, # ]; push(@loop, $ref); } # HTMLファイル出力 $t{template} = HTML::Template->new(filename => 'm_start.htm'); $t{template}->param(LOOP => \@loop); print $t{template}->output; 1;

m_start.htm

<html> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <head><title>m_start</title> <head> <body bgcolor="#fcf5ca"> <h2>m_start</h2> <a href="http://localhost/index.html">http://localhost/index.html</a> <hr> <form method="POST" action="m_select.pl"> <table> <tr bgcolor=lightcyan> <th> NO </th> <th> QTY1 </th> <th> QTY2 </th> </tr> <TMPL_LOOP NAME="LOOP"> <tr bgcolor=lightcyan> <td> <TMPL_VAR NAME="NO"> </td> <td> <select name="SEL1"> <TMPL_LOOP NAME="LOOP2"> <option <TMPL_VAR NAME="N11">>1</option> <option <TMPL_VAR NAME="N12">>2</option> <option <TMPL_VAR NAME="N13">>3</option> <option <TMPL_VAR NAME="N14">>4</option> <option <TMPL_VAR NAME="N15">>5</option> </TMPL_LOOP> </select> </td> <td> <input type="text" name="SEL2"> </td> </tr> </TMPL_LOOP> </table> <input type="submit" value="選択"> <input type="reset" value="取消"> </form> <hr> </body> </html>

m_select.pl

use strict; use CGI qw/:standard/; use HTML::Template; my (%t,$n,@loop); # CGIのパラメータ転送 $t{q} = new CGI; @{ $t{select1} } = $t{q}->param("SEL1"); @{ $t{select2} } = $t{q}->param("SEL2"); # 2つのデータを選別 @{ $t{select} } = (); for $n ( 0 .. $#{ $t{select1} } ) { $t{select11} = $t{select1}[$n]; $t{select21} = $t{select2}[$n]; if ($t{select21} and $t{select21} > 5 ) { push(@{ $t{select} },$t{select21}); } else { push(@{ $t{select} },$t{select11}); } } @loop = (); for $n ( 0 .. $#{ $t{select} } ) { $t{NO} = $n + 1; my %row = ( id => $t{NO}, NO => $t{select}[$n] ); push(@loop, \%row); } # HTMLファイル出力 $t{template} = HTML::Template->new(filename => 'm_select.htm'); $t{template}->param(LOOP => \@loop); print $t{template}->output; 1;

m_select.htm

<html> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <head><title>m_select</title> <head> <body bgcolor="#fcf5ca"> <h2>m_select</h2> <a href="http://localhost/index.html">http://localhost/index.html</a> <hr> 選択結果は次のとおりである。<br> <table> <tr bgcolor=lightcyan> <th> id </th> <th> NO </th> </tr> <TMPL_LOOP NAME="LOOP"> <tr bgcolor=lightcyan> <td> <TMPL_VAR NAME="id"> </td> <td> <TMPL_VAR NAME="NO"> </td> </tr> </TMPL_LOOP> <table> <hr> <a href="http://localhost/scripts/m_start.pl">戻る</a> </body> </html> ---------------------------------------------------------------------------

以下是老程序,留作参考。

########################### # html_template_test.pl use strict; use HTML::Template; my(%t,@fld,$n); my $template = HTML::Template->new(filename => 'test0.htm'); my @loop = (); $t{htmfile} = 'test1.htm'; $template->param(LOOP => [ { name =>'Bobby', nicknames => [ { name => 'the big bad wolf'}, { name => 'He-Man' }, ], } ], ); open(OUT,">$t{htmfile}"); print OUT $template->output; close(OUT); print "The output file is $t{htmfile}\n"; __END__; --------------------------------------------------------------------------- <TMPL_LOOP NAME="LOOP"> <TMPL_VAR NAME="name"> <TMPL_VAR NAME="nicknames"> </TMPL_LOOP> ====> c:\database\perl>perl html_template_test.pl HTML::Template->output() : fatal error in loop output : HTML::Template::param() : attempt to set parameter 'nicknames' with an array ref - parameter is not a TM PL_LOOP! at C:/Perl/site/lib/HTML/Template.pm line 3068 at html_template_test.pl line 25 --------------------------------------------------------------------------- <TMPL_LOOP NAME="LOOP"> <TMPL_VAR NAME="name"> </TMPL_LOOP> HTML::Template->output() : fatal error in loop output : HTML::Template : Attempt to set nonexistent parameter 'nicknames' - this parameter name doesn't match an y declarations in the template file : (die_on_bad_params => 1) at C:/Perl/site/l ib/HTML/Template.pm line 3068 at html_template_test.pl line 16 --------------------------------------------------------------------------- <TMPL_LOOP NAME="LOOP"> name:<TMPL_VAR NAME="name"><br> <TMPL_LOOP name="nicknames"> nickname: <TMPL_VAR NAME="name"><br> </TMPL_LOOP> </TMPL_LOOP> </BODY> </HTML> ------------------------------------------------------ Name:Bobby ==>nickname: the big bad wolf ==>nickname: He-Man ------------------------------------------------------ ########################### # html_template_test.pl use strict; use HTML::Template; my(%t,@fld,$n,@loop); my $template = HTML::Template->new(filename => 'test0.htm'); @loop = (); $t{htmfile} = 'test1.htm'; my %row = ( name => 'Bobby__test', nicknames => [ { name => 'the big bad wolf__test'}, { name => 'He-Man_test' }, ], ); push(@loop, \%row); $template->param(LOOP => \@loop); open(OUT,">$t{htmfile}"); print OUT $template->output; close(OUT); print "The output file is $t{htmfile}\n"; __END__; --------------------------------------------------------------------------- # エンジン番号とエンジン名を抽出 $t{sth} = $t{dbh}->prepare("select id, name from main_type1"); $t{sth}->execute; while (@rec = $t{sth}->fetchrow_array) { push(@{ $t{type1_id} },$rec[0]); push(@{ $t{type1_name} },$rec[1]); } $t{sth}->finish; # パーツコードとパーツ名を抽出 $t{sth} = $t{dbh}->prepare("select id, name,code,engine,dwg,memo from parts1"); $t{sth}->execute; while (@rec = $t{sth}->fetchrow_array) { push(@{ $t{parts_name}{$rec[3]} },$rec[1]); push(@{ $t{parts_code}{$rec[3]} },$rec[2]); push(@{ $t{parts_dwg}{$rec[3]} },$rec[3]); push(@{ $t{parts_memo}{$rec[3]} },$rec[4]); } $t{sth}->finish; $t{dbh}->disconnect; for $n ( 0 .. $#{ $t{type1_name} } ) { $t{type1_name1} = $t{type1_name}[$n]; $t{type1_id1} = $t{type1_id}[$n]; $ref = (); for $n1 ( 0 .. $#{ $t{parts_name}{$t{type1_id1}} } ) { $t{parts_name1} = $t{parts_name}{$t{type1_id1}}[$n1]; $ref = [ { parts_name => $t{parts_name1} }, ], } my %row = ( type_name => $t{type1_name1}, LOOP2 => $ref ); push(@loop, \%row); } ===>問題:パーツは一列のみ出力。 --------------------------------------------------------------------------- <TMPL_LOOP NAME="LOOP"> <TMPL_VAR NAME="type_name"><br> <TMPL_LOOP NAME="LOOP2"> ==><TMPL_VAR NAME="parts_name"><br> </TMPL_LOOP> </TMPL_LOOP> --------------------------------------------------------------------------- $ref = [ { [ { parts_name => $t{parts_name1} } ] }, ], HTML::Template->output() : fatal error in loop output : HTML::Template->output() : fatal error in loop output : HTML::Template : Attempt to set nonexistent para meter 'array(0x1ef0dec)' - this parameter name doesn't match any declarations in the template file : (die_on_bad_params => 1) at C:/Perl/site/lib/HTML/Template. pm line 3068 at C:/Perl/site/lib/HTML/Template.pm line 3069 at show_parts1.pl line 77 --------------------------------------------------------------------------- 成功した! for $n ( 0 .. $#{ $t{type1_name} } ) { $t{type1_name1} = $t{type1_name}[$n]; $t{type1_id1} = $t{type1_id}[$n]; $ref = (); for $n1 ( 0 .. $#{ $t{parts_name}{$t{type1_id1}} } ) { $t{parts_name1} = $t{parts_name}{$t{type1_id1}}[$n1]; $t{parts_code1} = $t{parts_code}{$t{type1_id1}}[$n1]; $ref = [ { parts_name => $t{parts_name1}, parts_code => $t{parts_code1} }, ], my %row = ( type_name => $t{type1_name1}, LOOP2 => $ref ); push(@loop, \%row); } } --------------------------------------------------------------------------- <TMPL_LOOP NAME="LOOP"> <TMPL_VAR NAME="type_name"> <TMPL_LOOP NAME="LOOP2"> ==><TMPL_VAR NAME="parts_name">==><TMPL_VAR NAME="parts_code"><br> </TMPL_LOOP> </TMPL_LOOP> --------------------------------------------------------------------------- 単重ループでOK! for $n ( 0 .. $#{ $t{type1_name} } ) { $t{type1_name1} = $t{type1_name}[$n]; $t{type1_id1} = $t{type1_id}[$n]; $ref = (); for $n1 ( 0 .. $#{ $t{parts_name}{$t{type1_id1}} } ) { $t{parts_name1} = $t{parts_name}{$t{type1_id1}}[$n1]; $t{parts_code1} = $t{parts_code}{$t{type1_id1}}[$n1]; my %row = ( type_name => $t{type1_name1}, parts_name => $t{parts_name1}, parts_code => $t{parts_code1}, ); push(@loop, \%row); } } --------------------------------------------------------------------------- <TMPL_LOOP NAME="LOOP"> <TMPL_VAR NAME="type_name"> ==><TMPL_VAR NAME="parts_name">==><TMPL_VAR NAME="parts_code"><br> </TMPL_LOOP>
戻る