in reply to Re: Readable HTML tables
in thread Readable HTML tables
But I like pure functions. This approach leaves me no easy way to stick the generated HTML into a variable.
There's that print again. I said I was breaking those laws :)
But really, I did not intend to write re-usable or pure code. Just something that feels comfortable and is very easy to use. In practice, I rarely need HTML in a variable. I don't mind writing HTML manually for that one time per month :)our $print = sub { print shift }; sub table (&) { $print->("<table>\n"); shift->(); $print->("</table>\n +"); } sub row (&) { $print->("<tr>"); shift->(); $print->("</tr>\n"); } sub cell { $print->("<td>$_</td>") for @_; } table { row { cell "foo" } }; my $html = "<html><body>"; local $print = sub { $html .= shift }; table { row { cell "foo" } }; $html .= "</body></html>";
Alternatively, break the rules again and just open the scalar as if it were a file:
sub table (&) { print-"<table>\n"; shift->(); print "</table>\n"; sub row (&) { print "<tr>"; shift->(); print "</tr>\n"; } sub cell { print "<td>$_</td>" for @_; } table { row { cell "foo" } }; my $html = "<html><body>"; open my $fh, '>>', \$html; select $fh; table { row { cell "foo" } }; close $fh; $html .= "</body></html>";
I cannot see how to do nested tables at all, when the table function is written to print.
You shouldn't need nested tables. If you find yourself needing a table in another table, that is probably because one table is there just for the layout of the page. And such a table isn't created with something like this, because you need attributes badly.
I meant the code to be simple. To get there, I broke quite some unwritten rules about purity and reusability. It was not my intention to support anything more than printing simple tables.
Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Readable HTML tables
by Sidhekin (Priest) on Aug 26, 2003 at 08:14 UTC | |
by Juerd (Abbot) on Aug 26, 2003 at 09:49 UTC |