Template toolkit + foreach
Bonjour,
je suis entrain de développer une appli qui doit gérer le crontab de mon linux.
Donc je développe actuellement l'interface web.
Alors j'ai une pahe html avec un FORM qui envoie des variables dans un script grace au CGI. Et ce script renvoie une page html grace à HTML template. MAis j'ai un problème avec la gestion des tableaux. Je n'arrive pas à renvoyer l'ensemble des données du tableau à l'écran. Il n'affiche que la première ligne du tableau. Je pense que je ne manipule pas bien le foreach de template::toolkit.
Voilà tout mon code.
Ma page html avec mon form
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" >
<head>
<title>Bienvenue sur mon site !</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<form action="/perl/lib/afficher_block.html">
machine recherche : <input type="text" name="machinerech"><br>
<input type="submit" value="Ok"><br>
</form>
</body>
</html> |
mon scrpt perl : afficher_block.html
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
#!/usr/bin/perl -w
use Config::Crontab;
use strict;
use warnings;
use CGI;
use Template;
my ($cgi) = new CGI;
my ($comments) = $cgi->param("machinerech");
my $file = 'affiche_block.html';
my $vars = {
'machine' => \&recherches,
};
sub recherches {
my $block = new Config::Crontab::Block;
my $blocksi = new Config::Crontab::Block;
my @coment = new Config::Crontab::Event;
my $ct = new Config::Crontab;
my @Tab;
my $var;
$ct->read;
for (my $j=0;$j<5000;$j++){
($block) = $ct->select_blocks( -index => $j);
if (defined($block) && $block->dump =~ m/$comments/){
@Tab = ();
for my $obj ( $block->lines) {
push @Tab, $obj->dump;
return \@Tab;
}
}
#my $i = 0;
#if ( @Tab > 0 ) {
#foreach (@Tab) {
#$i++;
#$var = "$i $_";
# }
# }
#else{
#print "bien tu es faux \n";
#}
}
}
my $template = Template->new({
# where to find template files
INCLUDE_PATH => ['/var/www/perl/src', '/var/www/perl/lib'],
# pre-process lib/config to define any extra values
PRE_PROCESS => 'config',
});
$template->process($file, $vars)
|| die "Template process failed: ", $template->error(), "\n"; |
et enfin mon template
Code:
1 2 3 4 5 6 7 8 9 10
|
[% INCLUDE header
title = 'Template Toolkit CGI Test'
%]
<p> la machine que vous cherchez est
[% FOREACH Tab IN machine %]
[% Tab %]
[% END %]
</p>
[% INCLUDE footer %] |
Merci.