FantomX1
FantomX1

Reputation: 1711

Perl Template Toolkit - how to return the template output result into a variable?

Perl Template Toolkit - how to return the template output result a into variable, so that it can be later processed or placed into a html layout using in a controller.

Upvotes: 3

Views: 873

Answers (1)

FantomX1
FantomX1

Reputation: 1711

The answer is, the variable for the result output must be passed as a third parameter by reference

$template->process(
        'dir/template.tt',
        {
            'par1'      => $par1,
            'par2'     => $par2,
        },
        \$output
    )   

It is indirectly mentioned in the documentation, but it might not be obvious from the quite verbose and technical description (in my opinion being not clear, almost sounding like there is no option to pass it as variable unless it is a callback implementing print variable, which is not the case)

This value may be one of: a plain string indicating a filename which will be opened (relative to OUTPUT_PATH, if defined) and the output written to; a file GLOB opened ready for output; a reference to a scalar (e.g. a text string) to which output/error is appended; a reference to a subroutine which is called, passing the output as a parameter; or any object reference which implements a print() method, which will be called, passing the generated output as a parameter.

https://metacpan.org/pod/Template#process($template,-%5C%25vars,-$output,-%25options)

Even searching for it in google does not deliver the specific link result easily and only as very late results after multiple tries using query 'perl template toolkit output template contents in variable process'

Upvotes: 5

Related Questions