Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Learning Template Toolkit Module

by jedikaiti (Friar)
on Mar 10, 2010 at 20:50 UTC ( #827894=perlquestion: print w/ replies, xml ) Need Help??
jedikaiti has asked for the wisdom of the Perl Monks concerning the following question:

OK, guys, I'm playing with the Template Toolkit module for the first time, and digging through documentation, but it would really help me if I could find some simple sample code to look at. Something that deals with a simple template and spits it out to a file would be perfect. If it does anything else, that's awesome. So far I'm striking out in my quest. Can anyone here help me find something?

Thanks a million!
Kaiti

Comment on Learning Template Toolkit Module
Re: Learning Template Toolkit Module
by zwon (Monsignor) on Mar 10, 2010 at 20:58 UTC

      Yea, I've been digging through the documentation on the site, but so far it's not all that helpful. Great on making the templates work, but not so much for getting it to go to a file instead of STDOUT. Either that or I'm missing it.

      I know the answer is probably something unbelievably simple, but I am just learning Perl, too, so I do sometimes need the obvious pointed out to me.

      Here, let me include the (short) code I am playing with as a learning exercise ATM:
      #!/usr/bin/perl -w
      use Template;
      my %info = ( ProjName => 'Test Project',
      DocName => 'Test Document' );
      print ("Hello!\n");
      open(INPUT,"start.pm") || die "Can't open input file: $!";
      open(OUTPUT,">testout.txt") || die "Sorry, no can do: $!";
      while(<INPUT>) {
      print OUTPUT $_;
      }
      my $tt = Template->new;
      $tt -> process('template', \%info) || die $tt->error;
      print OUTPUT $tt;
      close(INPUT) || die "Can't close input : $!";
      close(OUTPUT) || die "Can't close output : $!";

      Comments on anything else I could be doing better are, of course, appreciated as well.

      Thanks again!
      Kaiti

        Yea, I've been digging through the documentation on the site, but so far it's not all that helpful. Great on making the templates work, but not so much for getting it to go to a file instead of STDOUT. Either that or I'm missing it.

        Its definitely all there:

        Documentation for the ->process method

        Quoted here:

        By default, the processed template output is printed to STDOUT. The process() method then returns 1 to indicate success. A third parameter may be passed to the process() method to specify a different output location. 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 (e.g. IO::Handle, Apache::Request, etc.) which will be called, passing the generated output as a parameter.

Re: Learning Template Toolkit Module
by Your Mother (Canon) on Mar 10, 2010 at 21:04 UTC
Re: Learning Template Toolkit Module
by FunkyMonk (Canon) on Mar 10, 2010 at 21:08 UTC
Re: Learning Template Toolkit Module
by trwww (Priest) on Mar 10, 2010 at 22:22 UTC

    Hello,

    Here is a one-liner that gives you the gist of how it works:

    $ perl -MTemplate -le 'Template->new->process(\"foo is: [% foo %]\nbaz +z is: [% bazz %]", {foo => "bar", bazz => "quxx"})' foo is: bar bazz is: quxx

    The documentation has plenty of examples, so methinks you're just not looking at what is sitting in front of you. There is also a book you can get if that makes it easier.

    regards,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://827894]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2014-09-16 02:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (155 votes), past polls