in reply to How? Use new() from HTML::Template

Here's an example:

#!/usr/bin/perl use strict; use warnings; use HTML::Template; my $template_file = '<TMPL_VAR NAME="PERLCODE">'; my $t = HTML::Template->new( scalarref => \$template_file ); $t->param( PERLCODE => 'Perl code goes here' ); print $t->output;

Replies are listed 'Best First'.
Re^2: How? Use new() from HTML::Template
by newbie00 (Beadle) on Jul 24, 2010 at 06:42 UTC

    Now ahmad, that looks clearer than what I read on the CPAN page. I'll give it a try.

    Thanks!

      Hello again.

      BACKGROUND:
      I'm trying to use that code in ahmad's reply to add reCaptcha to a form in a template file. That template file is a static file, not a Perl executable file like .pl nor .pm.

      ===

      Within the form code section in that template file, I added a placeholder in hopes of adding and executing a single Perl statement once that template file was called by the .pm file. I've renamed that placeholder from <TMPL_VAR NAME="PERLCODE"> to <RECAPTCHA>.

      I tried adding the Perl statements as shown below inside of the .pm file that calls the template file, but with each version I tried, it generated a 'Software Error' pointing to the '$t->param...' statement. There's other code prior to this code I added.

      use HTML::Template; use Captcha::reCAPTCHA; my $c = Captcha::reCAPTCHA->new; my $t = HTML::Template->new( scalarref => \$template_file ); $t->param( <RECAPTCHA> => "print $c->get_html( 'your public key here' +);" ); $Output = $t->output;
      (NOTE: I substituted the key for 'your public key here'.)

      This routine is part of a subroutine where the final $Output var is returned.

      What might be done to get this to work?

      Thanks.
        I've renamed that placeholder from <TMPL_VAR NAME="PERLCODE"> to <RECAPTCHA>.
        Now it's not an HTML::Template placeholder so H::T won't know what it is.
        use HTML::Template; use Captcha::reCAPTCHA; my $c = Captcha::reCAPTCHA->new; my $html = $c->get_html('your public key here'); my $t = HTML::Template->new(scalarref => \$template_file); $t->param(html => $html); $output = $t->output;
        Change your template placeholder to <TMPL_VAR NAME="html">.

        Untested.

        You can't put Perl code in a H::T template. That, imo, is A Good Thing. Keep all the code in your script and leave H::T to look after the presentation (the view).

        It's worth having a good read of the docs. While you can't put code in the template there are some nifty things you can do that will make your life a lot easier.