Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Monks!
I am testing with tt and there is a "1" been printing at the end of the page, why is this happening?
Here is the sample code that prints 1 at the end of the page:

page.pl
#!/usr/bin/perl -w use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use Template; my $q = CGI; my $tt = Template->new({ INCLUDE_PATH => '../../temp/'}); my $vars = {}; $vars->{'title'} = 'Template Test'; $vars->{'site_name'} = 'TT TEST Page'; print $q->header(); print $tt->process('page.tt', $vars) || die $tt->error(); exit 1;

page.tt
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>[% title %]</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1 +"> </head> <body> <p>[% site_name %]</p> <br> Test Page <br> </table> </body> </html>
Thanks for the explanation!

Replies are listed 'Best First'.
Re: Printing a strange 1 on page.
by ikegami (Patriarch) on Dec 15, 2011 at 04:10 UTC

    "The process() method then returns 1 to indicate success", which you proceed to print. (Don't print it if you don't want to see it.)

      OK, thanks, but how can I avoid the 1 of been printed to the page?
        From the TT Docs:
        By default, the processed template output is printed to STDOUT. The process() method then returns 1 to indicate success.
        Just get rid of the "print" in front of "$tt->process(..", and your "1" will disappear.

                    "XML is like violence: if it doesn't solve your problem, use more."

Re: Printing a strange 1 on page.
by 3dbc (Monk) on Dec 15, 2011 at 03:32 UTC
Re: Printing a strange 1 on page.
by Alyssaly (Initiate) on Dec 15, 2011 at 07:35 UTC
    Ok,I should be honest to say:It is hard for me to understand!

      ikegami:

      "The process() method then returns 1 to indicate success", which you proceed to print. Don't print it if you don't want to see it.

      Just to be perfectly clear, the statement printing return value of  process() method:
          print $tt->process('page.tt', $vars) || die $tt->error();
      A statement not printing return value of  process() method:
          $tt->process('page.tt', $vars) || die $tt->error();

      What are you struggling to understand? As indicated by NetWallah above the default behaviour of TT is to "print" its output so calling code doesn't need to (in fact as you have observed, shouldn't) print any "output".

      What you are printing is the success result returned by process - don't do that.

      True laziness is hard work