hesco has asked for the wisdom of the Perl Monks concerning the following question:
My test file includes:
->send_report() merely dispatches to an appropriate method based on report type.$reporter = My::Latest::Module::Reports->new(\%defaults); my $result = $reporter->send_report({ type => 'nightly_summary' }); print STDERR Dumper($result);
My output reads:sub _nightly_summary { my $self = shift; my $tmpl_dir = $self->{'cfg'}->param("templates.template_dir"); my $tmpl = $self->{'cfg'}->param("rpt_tmpl.nightly_summary"); my $sql = "SELECT substr(NOW(),1,10);"; my $sth = $self->{'dbh_pb'}->prepare($sql); $sth->execute(); my ($date) = $sth->fetchrow_array(); $date = '2008-12-05'; # for testing after midnight my $my_template = "$tmpl_dir/$tmpl"; my $output; # my $output = '/home/hesco/nightly_summary.txt'; print STDERR "The template is: $my_template \nThe output file is: $o +utput \n"; my $stats = $self->_compute_rpt_stats($date); my $template = Template->new(); # $template->process($my_template,$stats); $template->process($my_template,$stats,$output); print STDERR Dumper($output); # return $stats; } sub _compute_rpt_stats { my $self = shift; my $date = shift; my ($stats,$sql,$sth,$sth1,$result,@loop_data); my $dbh = $self->{'dbh_pb'}; my $sql_stats = $self->{'cfg'}->get_block("stats"); my $sql_stats_today = $self->{'cfg'}->get_block("stats_today"); # print STDERR Dumper(\$sql_stats); my @statistics = qw/shifts dialed spoken messages supporters new_sub +scribers yard_signs call_backs contributions pledges/; foreach my $statistic (@statistics){ # print "The next statistic is: $statistic.\n"; $sql = $sql_stats->{'sql_stats_' . $statistic}; if(length($sql) > 3){ # print STDERR $sql,"\n"; $sth1 = $dbh->prepare($sql); $sth1->execute(); ($stats->{'team'}->{$statistic}) = $sth1->fetchrow_array() || 'u +nknown'; } else { $stats->{'team'}->{$statistic} = 'unknown'; } $sql = $sql_stats_today->{'sql_stats_today_' . $statistic}; if(length($sql) > 3){ $sth1 = $dbh->prepare($sql); $sth1->execute($date); ($stats->{'team_today'}->{$statistic}) = $sth1->fetchrow_array() + || 'unknown'; } else { $stats->{'team_today'}->{$statistic} = 'unknown'; } } $sql = $self->{'cfg'}->param("sql_rpt.sql_rpt_vols_worked_today"); $sth = $self->{'dbh_pb'}->prepare($sql); my @loop_data_worked_today = (); $sth->execute($date) or die "Unable to execute $sql."; while (my $row_hashref = $sth->fetchrow_hashref()){ my %row_data = (); $row_data{'NAME'} = $row_hashref->{'name'}; $row_data{'LOG_IN_DATE'} = $row_hashref->{'log_in_date'}; $row_data{'NUMS_ASSIGNED'} = $row_hashref->{'nums_assigned'}; push (@loop_data_worked_today, \%row_data); } $stats->{'loop'}->{'VOLS_WORKED_TODAY'} = \@loop_data_worked_today; # etc., etc., etc. # print Dumper(\$stats); return $stats; }
And a sample of the template reads:The template is: /etc/canvas/tmpl/rpt/nightly_summary.tmpl The output file is: $VAR1 = undef; $VAR1 = 1;
The current Message of the Day reads: <tmpl_var name=MOD> Overall our phone canvass team has worked <tmpl_var name=TEAM_SHIFTS> phone bank shifts, dialed <tmpl_var name=TEAM_DIALED> phone numbers, spoken with <tmpl_var name=TEAM_SPOKEN> voters, left messages with <tmpl_var name=TEAM_MSGS> households, identified <tmpl_var name=TEAM_SUPPORTERS> supporters, added <tmpl_var name=TEAM_SUBSCRIBERS> subscribers to our campaign lists, placed <tmpl_var name=TEAM_YARD_SIGNS> yard signs, scheduled <tmpl_var name=TEAM_CALL_BACKS> call backs, raised $<tmpl_var name=TEAM_CONTRIBUTIONS> in contributions plus $<tmpl_var name=TEAM_PLEDGES> in pledges. The following Phone Bank Volunteers Worked today: <tmpl_loop name="VOLS_WORKED_TODAY"> <tmpl_var name=CALLER_NAME> <tmpl_var name=NUMS_ASSIGNED_TODAY> </tmpl_loop> Volunteers Identified today: <tmpl_loop name="VOLUNTEERS_ID_TODAY"> <tmpl_var name=VOTER_NAME>; <tmpl_var name=VOTER_ADDRESS> <tmpl_var name=VOTER_PHONE>; <tmpl_var name=VOTER_EMAIL> Voter Registration Number: <tmpl_var name=VOTER_ID>; VoterID Code: <tm +pl_var name=VOTER_ID_CODE>; Yardsign: <tmpl_var name=YARDSIGN>; Call +again?: <tmpl_var name=FOLLWUP_CALL> \n Comments: <tmpl_var name=COMMENTS> \n </tmpl_loop>
My question though is: what happened to my output? How do I find it? Dumping my $stats hashref shows useful data, well organized. My template seems to be there. What happened to the marriage between the two?
-- Hugh
UPDATE:
Folks below nailed it. Thanks. I was the victim of my own early morning hours sloppy cut-n-paste errors, incompletely adapting from an HTML::Template script to a Template::Toolkit script. With Corion's help, I saw this issue early this morning and started building a new test script which is giving me some anticipated output. I'm now getting most of the single statistics, I keep seeing "file error - parse error . . . unexpected token (mod)" messages when I try to add that in. And after some sleep, I'm going to start now on making those loops work for me the TT way.
Thanks everyone who pointed the way.
ANOTHER UPDATE:
Success! I got the loops working and this is now generating a useful (if still somewhat ugly) report. I've moved what I learned from my test script into the module and tested it there and all seems to be fine with the world.
Next step: this template is intended for use in generating an email report from a cron job. How do I make my template generate wrapped paragraphs suitable for that use?
YET ANOTHER UPDATE:
Thank you, genehack. That was the ticket. Four tests later and this seems to be working. Thanks, but I've got a MIME::Lite based module set up to send the email. My question was focused more on getting the copy formatted appropriately to feed to that module.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Looking for output from Template::Toolkit
by almut (Canon) on Dec 06, 2008 at 10:02 UTC | |
|
Re: Looking for output from Template::Toolkit
by genehack (Beadle) on Dec 06, 2008 at 13:29 UTC | |
|
Re: Looking for output from Template::Toolkit
by genehack (Beadle) on Dec 07, 2008 at 15:12 UTC |