Thread HTC und HTML-Tabellen: zweidimensionales LOOP (5 answers)
Opened by MartinR at 2007-07-05 13:25

bloonix
 2007-07-05 22:21
#276 #276
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Ich habe eine gewisse Vorstellung von dem was du möchtest und vielleicht
ist das folgende Stück etwas für dich... wenn es stimmt, dann müsstest
du es nur für HTML-Template-Compiled abändern.

Code: (dl )
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
use strict;
use warnings;
use Template;

my @rows = (
  { foo => 1, bar => 1, baz => 1 },
  { foo => 2, bar => 2, baz => 2 },
  { foo => 3, bar => 3, baz => 3 },
  { foo => 4, bar => 4, baz => 4 },
);

my @titles = sort keys %{$rows[0]};

my $t = Template->new();
$t->process(\*DATA, { rows => \@rows, titles => \@titles });

__END__
<table>
  <tr>
  [%- FOREACH title = titles %]
     <th>[% title %]</th>
  [%- END %]
  </tr>
  [%- FOREACH terms = rows %]
  <tr>
     [%- FOREACH term = terms.keys.sort %]
     <td>[% terms.$term %]</td>
     [%- END %]
  </tr>
  [%- END %]
</table>


Output:
<table>
  <tr>
     <th>bar</th>
     <th>baz</th>
     <th>foo</th>
  </tr>
  <tr>
     <td>1</td>
     <td>1</td>
     <td>1</td>
  </tr>
  <tr>
     <td>2</td>
     <td>2</td>
     <td>2</td>
  </tr>
  <tr>
     <td>3</td>
     <td>3</td>
     <td>3</td>
  </tr>
  <tr>
     <td>4</td>
     <td>4</td>
     <td>4</td>
  </tr>
</table>
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.

View full thread HTC und HTML-Tabellen: zweidimensionales LOOP