in reply to Re^2: HTML::Template question
in thread HTML::Template question

That's not what I want to get from the Template
I was pointing out where the problem is, and showing you that your template has conflicting requirements. You will need to change both your template and the way you construct the data structure. If you can show a correct template it should be possible to advise on the data structure.

Replies are listed 'Best First'.
Re^4: HTML::Template question
by iRemix94 (Sexton) on Jul 21, 2015 at 13:41 UTC

    Sorry, I am very new to PERL and don't know exactly what's the problem here. Could you explain my problem a little bit more? All I want is that table with X amounts of rows and columns.

      You construct SCHEDULE_ROWS and SCHEDULE_COLUMNS in two different loops:
      # SCHEDULE_ROWS for my $dif_schedule(@schedule) { my %schedule_rows_data; $schedule_rows_data{SCHEDULE}=$dif_schedule; push (@schedule_rows_loop, \%schedule_rows_data); } # SCHEDULE_COLUMNS for(my $i=0;$i<=$scheduled_export_days;$i++) { my %schedule_columns_data; $schedule_columns_data{SCHEDULE_COLUMNS_COUNT}=1; push (@schedule_columns_loop, \%schedule_columns_data); } # ...later $data_structure{SCHEDULE_ROWS} = \@schedule_rows_loop; $data_structure{SCHEDULE_COLUMNS} =\@schedule_columns_loop;
      For your table to work, SCHEDULE_COLUMNS needs to be a key within SCHEDULE_ROWS, so you need to construct it within the first loop, but without knowing your actual data I can't tell how to do that.
Re^4: HTML::Template question
by iRemix94 (Sexton) on Jul 21, 2015 at 13:54 UTC

    I updated the template file, for some reason the IMPORT loop wasnt the same as the export loop, my mistake. Maybe you can give me a tip how I can fix my data structure?

      Maybe something like this:
      for my $dif_schedule ( @schedule ) { my @cols; for my $i ( 0 .. $scheduled_export_days ) { push( @cols, { SCHEDULE => $dif_schedule } ); } push ( @schedule_rows_loop, { SCHEDULE_COLUMNS => \@cols } ); }

        Thank you, that helped me a lot!

      This is my best guess at what your source data is and how to process it.

      #!perl use strict; use HTML::Template; use Data::Dump 'pp'; my %bi = ( 'Customer 1'=>{ 'Domain 1'=>{ 'Host 1'=>{ BACKUPCFG=>{ EXPORT_SCHEDULE_1=>{ VALUE => '01:00,02:00,03:00' }, IMPORT_ACTIVE_1=>{ VALUE => 1 }, EXPORT_ACTIVE_1=>{ VALUE => 1 }, EXPORT_BACKUP_LIFETIME_SEC_1=>{ VALUE => 3600*24*3 }, # 3 da +ys } }, }, 'Domain 2'=>{ 'Host 2'=>{ BACKUPCFG=>{ EXPORT_SCHEDULE_1=>{ VALUE => '05:00,06:00,07:00,08:00' }, IMPORT_ACTIVE_1=>{ VALUE => 1 }, EXPORT_ACTIVE_1=>{ VALUE => 1 }, EXPORT_BACKUP_LIFETIME_SEC_1=>{ VALUE => 3600*24*4 }, # 4 da +ys } } }, }, ); my $customer = "Customer 1"; my $tr_bgcolor = '#0000ff'; my @data_structure_loop = (); for my $domain (sort keys %{$bi{$customer}}) { my @schedule_rows_loop=(); my %data = ( CUSTOMER => $customer, DOMAIN => $domain, ); my @hosts = sort keys %{$bi{$customer}{$domain}}; for my $host (@hosts) { my $hr = $bi{$customer}{$domain}{$host}->{BACKUPCFG}; my $schedule = $hr->{EXPORT_SCHEDULE_1}->{VALUE}; if (defined $schedule){ ## Da nicht alle Kunden / Domainen zurzeit eine EXPORT_SCHEDULE +haben $data{IMPORT_VALUE} = $hr->{IMPORT_ACTIVE_1}->{VALUE}; $data{EXPORT_VALUE} = $hr->{EXPORT_ACTIVE_1}->{VALUE}; my $t = $hr->{EXPORT_BACKUP_LIFETIME_SEC_1}->{VALUE}; my $scheduled_export_days = convert_seconds_days($t); my @schedule = split /,/, $schedule; for my $dif_schedule (@schedule) { my @schedule_columns_loop = (); for my $i (0..$scheduled_export_days){ push @schedule_columns_loop, {SCHEDULE => $dif_schedule}; } push @schedule_rows_loop, {SCHEDULE_COLUMNS => \@schedule_colu +mns_loop }; } } } $data{BGCOLOR_CUSTOMER} = $tr_bgcolor; $data{SCHEDULE_ROWS} = \@schedule_rows_loop; push @data_structure_loop, \%data; #pp \%data; } my $template = HTML::Template->new_filehandle(\*DATA); $template->param(DATA_STRUCTURE => \@data_structure_loop); open HTM,'>','test.htm' or die "$!"; print HTM $template->output; sub convert_seconds_days { my $s = shift; return int $s/(3600*24); } __DATA__ <html> <head></head> <body> <table border="1" cellpadding="3" cellspacing="0"> <TMPL_LOOP NAME=DATA_STRUCTURE> <TMPL_IF NAME=BGCOLOR_CUSTOMER> <tr> <TMPL_ELSE> <tr bgcolor="white"> </TMPL_IF> <th><TMPL_VAR NAME=CUSTOMER></th> <th><TMPL_VAR NAME=DOMAIN></th> <td> <TMPL_IF NAME=EXPORT_VALUE> <table border="1" cellpadding="3" cellspacing="0"> <TMPL_LOOP NAME=SCHEDULE_ROWS> <tr> <TMPL_LOOP NAME=SCHEDULE_COLUMNS> <td><b><TMPL_VAR NAME=SCHEDULE></b></td> </TMPL_LOOP> </tr> </TMPL_LOOP> </table> </TMPL_IF> </td> <td> <TMPL_IF NAME=IMPORT_VALUE> <table border="1" cellpadding="3" cellspacing="0"> <TMPL_LOOP NAME=SCHEDULE_ROWS> <tr> <TMPL_LOOP NAME=SCHEDULE_COLUMNS> <td><b><TMPL_VAR NAME=SCHEDULE></b></td> </TMPL_LOOP> </tr> </TMPL_LOOP> </table> </td> </TMPL_IF> </tr> </TMPL_LOOP> </table> </body> </html>
      poj

      Give you a hint? Not without code and data. And don't give us those, as you did above, by changing a prior posting. At this level (Re^5), scrolling back up 2 or 3 levels just doesn't work for me... and I suspect it doesn't work for many others.

      Added note: when you change prior posts, use strikethrus rather than deleting, and plainly mark what's new.