in reply to Template Toolkit, and delaying the execution of a function

I see no reason in your code to delay execution of show_elements. You can just change that sub to return a list of stuff, instead of having it output HTML items directly (which is the whole point of using templates in the fist place):
sub show_elements ($)
{
    my $num = $_[0];
    return [] unless( $num > 0 );
    return [ 1..$num ];
}
...
my $data = {
             show_elements => show_elements( $elements ),
           };
then in the template:
<ul>
{# FOREACH e = show_elements #}
  <li>{# e #}</li>
{# END #}
</ul>

If you really need to delay the sub call, have a look at the TT docs which explain it quite adequately.

  • Comment on Re: Template Toolkit, and delaying the execution of a function

Replies are listed 'Best First'.
(elbie 2): Template Toolkit, and delaying the execution of a function
by elbie (Curate) on Aug 18, 2001 at 18:38 UTC
    You're not the first person to suggest changing the subroutine. I'd like to clarify that that is exactly the thing which I was hoping to avoid.

    The example I gave above was a rather simplified example. Some of the subroutines are rather long and complex, and with the current allotment of time, it's not feasible.

    Any new stuff we will write will certainly take advantage of all the fine features that the TT has to offer, but for the existing stuff, I need something quick and dirty.

    dondelecaro suggested IO::Scalar which seems promising, and I like perrin's suggestion as well. I'll try out both of those.

    elbieelbieelbie