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


I have a template in which i populate router and interfaces (<TMPL_VAR NAME>)
In the tmpl i want to generate a link with the above variables as parameters <a href = "display_graph.cgi?router=<TMPL_VAR NAME="router">&interface=<TMPL_VAR NAME="interface"></a>
Above does not work how do i get the variables passed as params to another script
See full code below

Database
site router interface dlh 138.188.193.233 FastEthernet0/0 dlh 138.188.193.233 Serial0/0

script that populates the tmpl
use strict; use warnings; use DBI; use HTML::Template; use Log::Log4perl; Log::Log4perl->init("/home/ndb/config/dlh_ndb_log.conf"); my $log = Log::Log4perl->get_logger(); my $template = new HTML::Template(filename=>"home1.tmpl"); my $dbh = DBI->connect('dbi:mysql:ndb','esm','esm') || die "Cannot con +nect"; #print "\n Connected....."; my @loop_data; my @sites; my @routers; my $query = "select distinct site_name from sites order by site_name"; my $sth = $dbh->prepare($query); $sth->execute; my $query_router = "select distinct router from sites where site_name += ?"; my $sth_router = $dbh->prepare($query_router); my $query_interface = "select interface from sites where site_name = ? + and router = ?"; my $sth_interface = $dbh->prepare($query_interface); while(@sites = $sth->fetchrow_array) { my @r_loop_data; $sth_router->bind_param(1,$sites[0]); $sth_router->execute; while(@routers = $sth_router->fetchrow_array) { my @interfaces; $sth_interface->bind_param(1,$sites[0]); $sth_interface->bind_param(2,$routers[0]); $sth_interface->execute; while(my @arr = $sth_interface->fetchrow_array) { my %interface_row = ( interface => $arr[0] ); push(@interfaces, \%interface_row); } my %row; %row = ( router => $routers[0] , interfaces_loop => \@interfaces ); push(@r_loop_data,\%row); } my %site_row = ( site => $sites[0], routers_loop => \@r_loop_data ); push(@loop_data, \%site_row); } $template->param(caption => "ST Sites"); $template->param(sites_loop => \@loop_data); print "Content-Type: text/html\n\n"; print $template->output; $dbh->disconnect or $log->logwarn("Cannot disconnect from the database +: $DBI::errstr"); exit 0;
<html> <body LINK="blue" ALINK="green" VLINK="#4169e1"> <table border="0" CELLPADDING="20" CELLSPACING="20" WIDTH="1000"> <tr> <td align = "center"><TMPL_INCLUDE NAME = "/var/www/cgi-bin/nd +b_header.tmpl"></td> </tr> <tr> <td align = "center"> <table border="1" CELLPADDING="0" CELLSPACING="0" WIDTH="1 +00%"> <caption > <b><TMPL_VAR NAME = "caption"></b> </h2> <TMPL_LOOP NAME="sites_loop"> <tr bgcolor = "cornflowerblue" text = "white"> <th colspan = "2" align = "left"><font color="white"> +<TMPL_VAR NAME = "site"></font></th> </tr> <TMPL_LOOP NAME="routers_loop"> <tr bgcolor = "powderblue"> <td align = "left"><font color = "navy"><b><TMPL_V +AR NAME="router"></b></font></td> <td align = "left"><TMPL_LOOP NAME = "interfaces_loop" +><a href = "display_graph.cgi?router=pdel246&interface=FastEthernet0/ +0"><TMPL_VAR NAME="interface"></a> | </TMPL_LOOP></td> </tr> </TMPL_LOOP> </TMPL_LOOP> </table> </td> </tr> <tr> <td align = "center"><TMPL_INCLUDE NAME = "/var/www/cgi-bin/nd +b_footer.tmpl"></td> </tr> </table> </body> </html>

In the above tmpl i have hard coded the call to display_cgi? how do i get the links to generate based on tmpl population.
Thanks,
chimni
UPDATE Problem solved by making the template variabls global while creating HTML::Template object

Replies are listed 'Best First'.
Re: HTML::Template : generation of links
by jeffa (Bishop) on Apr 12, 2004 at 16:15 UTC
    Looks like you have a lot of bad data in your database:
    select distinct site_name from sites order by site_name;
    select distinct router from sites where site_name = ?;
    select interface from sites where site_name = ? and router = ?
    
    but i don't really see much that we can do about it right now. In the meantime, why don't you try a simpler approach instead. Untested: Select dictinct routers from a site, and then trying to only compare one of those to find the correct interface is surely not correct. You really should try to clean up your data first, garbage in --- garbage out. Give this code a shot and see what happens. At the very least, remove the parts of your code that don't matter. Don't worry about table formatting until you have correct data. Simplify. And read the HTML::Template Tutorial if you already haven't.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)