Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Looking for output from Template::Toolkit

by hesco (Deacon)
on Dec 06, 2008 at 09:26 UTC ( [id://728527]=perlquestion: print w/replies, xml ) Need Help??

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

This is my first (well perhaps my second) foray into Template::Toolkit.

My test file includes:

$reporter = My::Latest::Module::Reports->new(\%defaults); my $result = $reporter->send_report({ type => 'nightly_summary' }); print STDERR Dumper($result);
->send_report() merely dispatches to an appropriate method based on report type.

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; }
My output reads:

The template is: /etc/canvas/tmpl/rpt/nightly_summary.tmpl The output file is: $VAR1 = undef; $VAR1 = 1;
And a sample of the template reads:

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>
So it appears that $output, once dumped is undefined, and that the $result returned by this ->_nightly_summary() method is true.

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.

if( $lal && $lol ) { $life++; }

Replies are listed 'Best First'.
Re: Looking for output from Template::Toolkit
by almut (Canon) on Dec 06, 2008 at 10:02 UTC
    <tmpl_var name=...

    Maybe I'm misunderstanding something (I've only taken a cursory look so far), but your template looks more appropriate for HTML::Template than for Template Toolkit.  (Update: "more appropriate" in the sense that Template Toolkit doesn't use tmpl_var / tmpl_loop etc. syntax — not that it couldn't handle the task at hand per se...)

Re: Looking for output from Template::Toolkit
by genehack (Beadle) on Dec 06, 2008 at 13:29 UTC

    First, that is indeed a HTML::Template template, not a TT one.

    Second, the way you're calling the Template process() method is wrong -- $output is undef, since you've commented that line out. Try having a look at the 'process' section of the Template perldoc...

Re: Looking for output from Template::Toolkit
by genehack (Beadle) on Dec 07, 2008 at 15:12 UTC

    If you just want to wrap the paragraphs at a certain line width, have a look at Template::Plugin::Wrap, which does just that.

    Otherwise, if that's not what you're looking for, could you rephrase your question below? Are you looking for information on how to send the mail itself, or suggestions on post-processing the template output, or ... ?

    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?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://728527]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-25 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found