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
m_start
m_start
http://localhost/index.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
m_select
m_select
http://localhost/index.html
選択結果は次のとおりである。
id
|
NO
|
|
|
戻る
---------------------------------------------------------------------------
以下是老程序,留作参考。
###########################
# 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__;
---------------------------------------------------------------------------
====>
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
---------------------------------------------------------------------------
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
---------------------------------------------------------------------------
name:
nickname:
------------------------------------------------------
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);
}
===>問題:パーツは一列のみ出力。
---------------------------------------------------------------------------
==>
---------------------------------------------------------------------------
$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);
}
}
---------------------------------------------------------------------------
==>==>
---------------------------------------------------------------------------
単重ループで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);
}
}
---------------------------------------------------------------------------
==>==>
戻る