dirtdog has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I'm trying to use HTML::Template and CGI::Widget::Tabs to create a web page that displays results from one query on a Tab called "Football" for example, and then display the results of the other query called "Baseball" on it's own Tab.

I'm able to get a webpage with the data, but it's displayed on one page.

I substituted the baseball and football example in lieu of using actual business data

My pseudo code is as follows

#!/usr/bin/env perl use strict; use warnings; use DBI; use HTML::Template; use File::Basename; use POSIX 'strftime'; use CGI::Widget::Tabs; use CGI; my $basename = basename($0,".pl"); my $outfile="$ENV{'CAPS_OUTDIR'}/$basename.xls"; open (OUT_HTML_FILE, ">$ENV{'CAPS_OUTDIR'}/$basename.htm") or die "Can + not open output html file: $basename.htm\n"; ## output the content-type so the web server knows my $script="$ENV{'HOME'}/scripts/template.pl"; my $template = HTML::Template->new(filename => $script); $template->param(TITLE=>'ENV Spreadsheet'); my $dbname = 'xxxxx'; # $ENV{ORACLE_SID}; my $user = 'xxxxx'; my $passwd = 'xxxxx'; my $dbh = DBI->connect("dbi:Oracle:$dbname", $user, $passwd, {RaiseErr +or => 1}) or die "Oracle Connect Failed: ", DBI->errstr; my @reports = ('Football', 'Baseball'); my %queries = ( "Football" => qq{ select team as TEAM, url as URL from sports where sport = 'Football' }, "Baseball" => qq{ select team as TEAM, url as URL from sports where sport = 'Baseball' }, ); my $cgi = CGI->new; my $tab = CGI::Widget::Tabs->new; $tab->cgi_object($cgi); for (@reports) { my $sth = $dbh->prepare($queries{$_}); $sth->execute(); $tab->headings( qw/FOOTBALL BASEBALL/ ); $tab->wrap(3); # $tab->wrap(1); # |uncomment to see the effect of # $tab->indent(0); # |wrapping at 1 without indentation $tab->default("FOOTBALL"); $tab->display; my @headings; foreach (@{$sth->{NAME}}) { my %rowh; $rowh{HEADINGS} = $_; push @headings, \%rowh; } $template->param(HEADINGS=>\@headings); my @rows; while (my @data_row = $sth->fetchrow_array) { my %row; $row{TEAM} = $data_row[0]; $row{URL} = $data_row[1]; push @rows, \%row; } $template->param(ROWS=>\@rows); print OUT_HTML_FILE "Content-Type: text/html\n\n", $template->output; }

My Template is as follows

<html> <title><TMPL_VAR name=TITLE></title> <body> <table border="1"> <tr> <TMPL_LOOP NAME=HEADINGS> <th><TMPL_VAR NAME=HEADINGS></th> </TMPL_LOOP> </tr> <TMPL_LOOP NAME=ROWS> <tr> <td><TMPL_VAR NAME=TEAM></td> <td><A HREF="<TMPL_VAR NAME=URL>" target="_blank"><TMPL_VAR NA +ME=URL></A></td> </tr> </TMPL_LOOP> </table> </body> </html>

I cannot figure out how to get it so the data from each query is displayed on it's tab

I'm struggling with how to incorporate the template with the widget module

Any help would be greatly appreciated

Thank you

Replies are listed 'Best First'.
Re: Help with CGI::WIDGET::TABS
by poj (Abbot) on Aug 02, 2017 at 20:09 UTC

    Not sure what you are expecting but try this

    #!/usr/bin/perl use strict; use CGI::Widget::Tabs; use CGI; use DBI; use HTML::Template; my $cgi = CGI->new; my $tab = CGI::Widget::Tabs->new; $tab->cgi_object($cgi); $tab->headings( qw/Football Baseball/ ); $tab->wrap(3); $tab->default("Football"); my $heading = $tab->render; my $sql = q! SELECT team as TEAM, url as URL FROM sports WHERE sport = ? !; my $dbh = DBI->connect("dbi:Oracle:$dbname", $user, $passwd, {RaiseError => 1}) or die "Oracle Connect Failed: ", DBI->errstr; my $sth = $dbh->prepare($sql); $sth->execute($tab->active); my @rows; while (my @f = $sth->fetchrow_array) { push @rows, { TEAM => $f[0], URL => $f[1], }; } my $template = HTML::Template->new( filehandle => *DATA ); $template->param(TITLE=>"ENV Spreadsheet" ); $template->param(HEADINGS=>$heading, ROWS=>\@rows ); print "Content-Type: text/html\n\n",$template->output; __DATA__ <html> <title><TMPL_VAR name=TITLE></title> <style type="text/css"> table.tab { border-bottom: solid thin #C0D4E6; text-align: cente +r } td.tab { padding: 2 12 2 12; width: 80; background-color: #FA +FAD2 } td.tab_actv { padding: 2 12 2 12; width: 80; background-color: #C0 +D4E6 } td.tab_spc { width: 5 } td.tab_ind { width: 15 } </style> <body> <TMPL_VAR NAME=HEADINGS> <table border="1" cellpadding="5" cellspacing="0"> <TMPL_LOOP NAME=ROWS> <tr> <td><TMPL_VAR NAME=TEAM></td> <td><A HREF="<TMPL_VAR NAME=URL>" target="_blank"><TMPL_VAR NAME=UR +L></A></td> </tr> </TMPL_LOOP> </table> </body> </html>

    Update : celpading corrected to cellpadding

    poj

      thanks poj

      I'm getting HTML::Template::param() : attempt to set parameter 'headings' with a scalar - parameter is not a TMPL_VAR! at html.pl line 67

      Did you get yours to run?

        Yes mine runs, but notice I replaced this loop in your template

        <tr> <TMPL_LOOP NAME=HEADINGS> <th><TMPL_VAR NAME=HEADINGS></th> </TMPL_LOOP> </tr>

        with the single line

        <TMPL_VAR NAME=HEADINGS>
        poj