Thread HTML-Template-Compiled: Mit Loop AoH ausgeben (19 answers)
Opened by GwenDragon at 2012-01-25 09:54

GwenDragon
 2012-01-25 09:54
#155651 #155651
User since
2005-01-17
14533 Artikel
Admin1
[Homepage]
user image
Code:
Code (perl): (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
 my $htc = HTML::Template::Compiled->new(
            filename          => "$datadir/template/termine.html",
            tagstyle          => [ qw(+php) ],
            loop_context_vars => 1,
            use_expressions => 1,
        );
        my @row_names = qw( Datum Zeit Ort Veranstaltung);

        foreach my $i ( 0 .. @data - 1 ) {
            my ( $t ) = $data[ $i ]{ Veranstaltung } =~ s/\\n/\n/g;
            $textile =
              Text::Textile->new( disable_html => 1, flavor => 'html' );
            if ( $textile ) {
                $data[ $i ]{ Veranstaltung } =
                  $textile->process( $data[ $i ]{ Veranstaltung } );
            }
            else {
                warn( "$PACKAGE: Problem with Textile in Code : $t" );
                last;
            }
        }

        $htc->param(
            termine => {
                column_names => \@row_names,
                data         => \@data
            }
        );
        $str = $htc->output();

data ist dabei eien Ref auf das AoH @data:
Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[
          {
            "Veranstaltung" => "C-Workshop 2020 Testdorf",
            "Zeit" => "12:00",
            "Datum" => "01.02.2012",
            "Ort" => "Testdorf",
            "Veranstalter" => "Tim Towdi"
          },
          {
            "Veranstaltung" => "Perlen für Anfänger",
            "Zeit" => "18:00",
            "Datum" => "01.03.2012",
            "Ort" => "Perlsen",
            "Veranstalter" => "IHK Perlsen"
          }
        ];


Template:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<table>
<caption>Veranstaltungen</caption>
<thead>
<tr>
<?LOOP .termine.column_names?>
<td><?= _ ?></td><?/LOOP?>
</tr>
</thead>
<tbody>
<?LOOP .termine.data alias=termin?>
<tr>
<?LOOP .termine.column_names?>
<td><?= termin._ ?></td>
<?/LOOP?>
</tr>
<?/LOOP?>
</tbody>
</table>

erzeugt dann folgende Ausgabe:
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
<table>
<caption>Veranstaltungen für </caption>
<thead>
<tr>

<td>Datum</td>
<td>Zeit</td>
<td>Ort</td>
<td>Veranstaltung</td>
</tr>
</thead>
<tbody>

<tr>

<td></td>

<td></td>

<td></td>

<td></td>

</tr>

</tbody>
</table>


ein termin-Wert ist laut HTC-Dump:
Code: (dl )
1
2
3
4
5
6
7
{
"Veranstaltung" => "C-Workshop 2020 Testdorf",
"Zeit" => "12:00",
"Datum" => "01.02.2012",
"Ort" => "Testdorf",
"Veranstalter" => "Tim Towdi"
}



Leider lässt sich wie in Zeile 13 des Templates über die Loop und _ nicht auf den Hashwert von termin zugreifen.
Ich möchte halt nicht den key des Hashes angeben müssen.

Geht das überhaupt? Oder ist das ein Denkfehler von mir?
Last edited: 2012-01-25 10:14:56 +0100 (CET)
die Drachin, Gwendolyn


Unterschiedliche Perl-Versionen auf Windows (fast wie perlbrew) • Meine Perl-Artikel

View full thread HTML-Template-Compiled: Mit Loop AoH ausgeben