Long story. My team has a group of custom libraries for inserting values into an HTML template within perl. Problem is these libraries were written way back when perl4 was da bomb.

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


In reply to Template Toolkit, and delaying the execution of a function by elbie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.