Skip to content

Commit

Permalink
Matrix elements may be scalars or reference to hash, where 'v' holds the
Browse files Browse the repository at this point in the history
value and 'a' is a reference to a hash of HTML attributes (check the example
in perldoc)
  • Loading branch information
andrefs committed Feb 4, 2011
1 parent 02a1698 commit 9986fbf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
47 changes: 46 additions & 1 deletion lib/HTML/Auto.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,30 @@ Simple example:
h($m,$m),
);
Using attributes:
use HTML::Auto qw/matrix h v/;
my @cols = qw/c1 c2/;
my @lines = qw/l1 l2/;
my $data =
[
[
{v => 1, a => { style => 'background: green'}},
2
],
[
{v => 3, a => {class => 'foo'}},
{v => 4, a => {style => 'color: red'}}
]
];
my $m = matrix(\@cols,\@lines,$data);
print v(
h($m)
);
=head1 SUBROUTINES/METHODS
=head2 matrix
Expand All @@ -63,10 +87,31 @@ sub matrix {
$_ = ucfirst($_);
}

my $vals = [];
my $attrs = [];

foreach my $row (@$data){
my $vrow = [];
my $arow = [];
foreach(@$row){
if (ref($_)){
push @$vrow, $_->{v};
push @$arow, $_->{a};
}
else {
push @$vrow, $_;
push @$arow, undef;
}
}
push @$vals, $vrow;
push @$attrs, $arow;
}

my $vars = {
cols => $cols,
lines => $lines,
data => $data,
vals => $vals,
attrs => $attrs,
};
my $template_name = 'matrix';

Expand Down
8 changes: 6 additions & 2 deletions lib/HTML/Auto/Templates.pm
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,16 @@ td {
[% END %]
</tr>
[% i_c = 0 %]
[% FOREACH i IN data %]
[% FOREACH i IN vals %]
<tr>
<td class="fst">[% lines.shift -%]</td>
[% j_c = 0 %]
[% FOREACH j IN i %]
<td[% IF i_c == j_c %] class="mid"[% END %]>[% j %]</td>
<td[% IF i_c == j_c %] class="mid"[% END %]
[% FOREACH att IN attrs.$i_c.$j_c.keys %]
[% att %]="[% attrs.$i_c.$j_c.$att %]"
[% END %]
>[% j %]</td>
[% j_c = j_c + 1 %]
[% END %]
</tr>
Expand Down

0 comments on commit 9986fbf

Please sign in to comment.