in reply to CGI.pm HTML-Generation Methods Considered Useful

Personally we have a pretty table function that takes an array of array refs (as well as width and centre args). It does the alternate the colours thing (stylesheet based) and as it takes an array of array refs all you need to do is unshift a header into the return from fetchall_arrayref and there is your table. This lives in our UI.pm and using it is a single call $html = pretty_table( $ary_ref ) +/- unshifting headers. Benefits include a single simple call and stylsheeting so the theme remains consistent. Oh and the sum total code volume of our CGI parser, HTML::Template, UI.pm.... is still less than CGI.pm alone, and a lot faster (not that code volume or size make much difference to most users). Here is the code FWIW.

sub pretty_table { my ($ary, $width, $no_center) = @_; my $header = $ary->[0]; $width = $width ? qq!width="$width"! : ''; my $html = qq!<table border="0" cellpadding="2" cellspacing="1" $w +idth>\n!; my $cols = scalar @$header; $html .= get_row( $header, $cols, 'header' ); my $flip_flop = 1; for my $row_ref ( @$ary[1..$#{$ary}] ) { my $class = $flip_flop ? 'light' : 'dark'; $html .= get_row( $row_ref, $cols, $class ); $flip_flop ^= 1; } $html .= qq!<tr><td colspan="$cols" class="header">&nbsp;</td></tr +>\n!; $html .= "</table>\n"; $html = qq!<div align="center"><center>\n$html</center></div>\n! u +nless $no_center; return $html } sub get_row { my ( $row_ref, $cols, $class ) = @_; my $html = qq!<tr>!; for my $td ( 0.. $cols-1 ) { my $data = defined $row_ref->[$td] ? $row_ref->[$td] : '&nbsp +;'; $html .= qq!<td class="$class">$data</td>!; } $html .= "</tr>\n"; return $html }

cheers

tachyon

Replies are listed 'Best First'.
Re^2: CGI.pm HTML-Generation Methods Considered Useful
by friedo (Prior) on Jul 10, 2004 at 11:54 UTC
    That reminds me; I forgot to include an evil method for alternating row colors:

    my @colors = ( '#dddddd', '#eeeeee' ); $html .= $q->table( { -bgcolor => '#ffffff', -border => 1 }, $q->caption('Status Summary'), # beware beautiful nested maps ahead map { my $k = $_; $q->Tr( { -bgcolor => (push @colors, shift @colors) && $colors[0] }, $q->td( $k ), map { $q->td( $data->{$k}{$_} eq 'bad' ? { -bgcolor => '#ff0000' } : { -bgcolor => '#ffffff' }, $data->{$k}{$_} ) } sort keys %{ $data->{$k} } ) } sort keys %{ $data } );

    There is no particularly good reason to do this. After this, it's just for fun. :)

      Surely you meant to map to stylesheet classes? Does anyone still hardcode colors into their code these days? It is 2004 afterall....

      Do you find it vaguely inefficient to use 3000+ lines of code + your 18 to do what <30 lines of code (or one line as a call when you modularise it) will do just as well, faster and in a more transparent maintainable and consistent manner?

      Hmm does that sound biased? OK I admit it. I think CGI.pm has no place in generating HTML in the first place, and don't like the interface. You either love it or you hate it. I have used it. I do hate it. Ovid likes it and so does merlyn. What can I say? They are obviously misguided, or I have no idea what I am talking about. Or perhaps they use what works for them as I use what works for me. Use what works for you. TIMTOWDI.

      cheers

      tachyon

        I use it when I don't have a templating solution nearby, or even in combination with a templating solution. CGI.pm generates shortcuts for common things, like option lists and headers, but also handles the sticky form elements. I don't know of any other as-ubiquitous easy solution for handling sticky form elements.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

        In the real world, I generally only use it when I need a one-off solution to generate static HTML. I wouldn't use it in a live web application, unless it was some internal thing that only saw occasional use.