Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by dondelelcaro (Monk)
on Aug 18, 2001 at 04:26 UTC ( [id://105850]=note: print w/replies, xml ) Need Help??


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

You might have already realized this, but the problem is in the print qq(). Template Toolkit parses the template and runs called subroutines before the data is output to STDOUT, so you will get output from those functions that are called before the data is acutally displayed.

The solution is to adjust the functions that are being called to return the values that they would have printed instead... ala:
$stuff_to_print .= qq(<li>$_</li>\n) for( 1 .. $num ); return $stuff_to_print;
or similar. Unfortunatly that involves a rewrite of the code...

You might be able to get it to work by selecting Template::Perl::PERLOUT as the standard output in the function... there is some indication that that might work as well.. but I'm not an expert in TT by any means.

Replies are listed 'Best First'.
(elbie): Template Toolkit, and delaying the execution of a function
by elbie (Curate) on Aug 18, 2001 at 06:40 UTC
    Yup, and that's exactly the problem. What I'm hoping is that there is some way to delay the execution of the subroutine, or to encapsulate the subroutine in another function that redirects the output to a variable.

    I can't seem to find the module Template::Perl anywhere though. :(

    elbieelbieelbie

      It would be cleaner to change you code to return values, but TT does provide a directive that automatically ties STDOUT for you. Try this:

      {#PERL#} $stash->get('show_elements'); {#END#}

      Or just call &show_elements in some other way from within the PERL block.

        I tried a couple of different things with Template::Stash to get the output of the subroutine, but all seem to result in STDOUT from the subroutine to print at the top of the page.

        Here's my modified code:

        my $tmpl_stash = Template::Stash->new( { 'show_subcat' => \&show_subcat( $lpmkey ), } ); my $tmpl_data = { "${lpmkey}select" => ' selected', # 'show_subcat' => show_subcat( $lpmkey ), 'lpmcat' => $lpmkey, 'lpmcategory' => { 'l' => 'Logos', 'p' => 'Photos', 'm' => 'More', }->{ $lpmkey }, };

        And then I have a <code>{#PERL#} block like you suggested in your post. I also just tried running the subroutine from the block directly, avoiding using Template::Stash, but I'm still running into the same problem.

        Any help would be appreciated.

        elbieelbieelbie

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-18 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found