text::template - 'here document' << query

Sean O'Riordain seanpor at acm.org
Wed Aug 20 11:03:34 CDT 2003


Hi folks,

I'm using Text::Template for a cgi application, where the templates are 
html with little bits of perl...

I want to include a bit of javascript conditionally, within the 
template... something along the lines of...

[@-- if ($affiliate eq "b1") {
print <<EOM;
... javascript here...
EOM
} --@]

where affiliate is passed in as a key in a hash... and this is fine... 
it has the correct value etc...

... but this snippet doesn't work... I just get a "1" (true? - because 
affiliate does eq "b1"... ) but i want it to print the javascript not 
the result of the 'if' :-)

I think I'm missing something obvious here...  ideas anybody ?

There's got to be a prettier way than masses of ugly $OUT = 'yeuch!'; 
statements...

http://perl.plover.com/Template/Manual.html

cheers,
Sean


------------------------------------------------

sub text_template($$) {
     my $filename = shift;
     my $fillings = shift; # a hash ref
     my $template = Text::Template->new( # -type => FILE,
                                     DELIMITERS => [ '[@--', '--@]' ],
                                     -source => $filename,
#                                    -prepend => q{ use strict 'vars'; },
                                     -untaint => 1
                                     );
     if (defined($Text::Template::ERROR)) {
         cluck $Text::Template::ERROR;
     }
     my $ft = $template->fill_in( HASH => $fillings );

     if ( defined $ft) {
         # print $ft;
     } else {
         cluck "unable to fill in template :$Text::Template::ERROR";
     }
     return $ft;
}

-------------------------

called in the .cgi as...
sub test_text_template() {
     my %h = ();
     $h{affiliate} = "b1";
     my $t = text_template("../templates/wibble1.tmpl", \%h);
}






More information about the Belfast-pm mailing list