elbie has asked for the wisdom of the Perl Monks concerning the following question:
When I try and use the -w option in a cgi that uses these libraries, I run into all sorts of problems. So I went looking for CPAN modules that I could replace bits of our library with. For this particular thing, the Template Toolkit seems to perform quite well, with one exception:
The old libraries could populate template variables with the output of functions which were run when the template parser reached the variable.
So far, I have no problem with setting up the perl Template object and changing tags to fit the old html templates and doing simple replacements, but when I try and use a function, the function runs at the very beginning of the script and I see the output at the top of the page instead of at the proper point in the template. I've included a small example below:
First the template file:
<html> <head><title>{#page_title#}</title></head> <body> Here's a list of {#elements#} numbers:<br> <ul> {#show_elements#} </ul> </body> </html>
And the corresponding cgi:
#!/usr/bin/perl -w use strict; use CGI qw/:standard/; use Template; sub show_elements ($) { my $num = $_[0]; return unless( $num > 0 ); print qq(<li>$_</li>\n) for( 1 .. $num ); ''; } my $elements = 6; my $cgi = CGI->new; my $tt = Template->new( { START_TAG => quotemeta( '{#' ), END_TAG => quotemeta( '#}' ), } ); my $data = { page_title => 'Elbie Intl Numbers Inc.', elements => $elements, show_elements => \&show_elements( $elements ), }; print $cgi->header(); $tt->process( 'test.html', $data ) || die $tt->error;
So what I get is an enumerated list above the HTML headers, and SCALAR(0x82921d4) or similar where the list should go.
Granted, given enough time, I would just rewrite all the called subroutines, but for now I just need to drop something in place of my existing template functions.
Any help would be greatly appreciated.
elbieelbieelbie
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Template Toolkit, and delaying the execution of a function
by dondelelcaro (Monk) on Aug 18, 2001 at 04:26 UTC | |
by elbie (Curate) on Aug 18, 2001 at 06:40 UTC | |
by perrin (Chancellor) on Aug 18, 2001 at 09:15 UTC | |
by elbie (Curate) on Aug 20, 2001 at 21:47 UTC | |
by perrin (Chancellor) on Aug 21, 2001 at 03:42 UTC | |
Re: Template Toolkit, and delaying the execution of a function
by echo (Pilgrim) on Aug 18, 2001 at 15:02 UTC | |
by elbie (Curate) on Aug 18, 2001 at 18:38 UTC |