Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Template Toolkit: template paths, INCLUDE_PATH, error

by 7stud (Deacon)
on Jan 06, 2015 at 11:18 UTC ( [id://1112323]=perlquestion: print w/replies, xml ) Need Help??

7stud has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

Using Template Toolkit, I can only get a blank page to display after processing my template. Here is my simple template:
<!DOCTYPE html> <html> <head><title>Test</title></head> <body> <div>From template:</div> <div>[% message %]</div> </body> </html>
Here is the CGI program that tries to send that template to the browser:
#!/usr/bin/env perl use strict; use warnings; use Template; my $tname = 'templates/form.html'; my $vars = { message => "Hello World\n" }; my $template = Template->new({ INCLUDE_PATH => '/Users/7stud/perl_programs/cgi_projects/1proj') }); $template->process($file, $vars) or die "*MY* template process failed: ", $template->error(), "\n";
That program is essentially copied from the docs.

Here is my directory structure:

1proj/ ---cgi-bin/ ------create_form.pl -------simple_cgi.pl ---serverHTTP.py ---templates/ -------footer.html -------form.html -------header.html ------xform.html 2 directories, 8 files
Here is create_form.pl:
#!/usr/bin/env perl use strict; use warnings; use Template; my $tname = 'templates/form.html'; my $vars = { message => "Hello World\n" }; my $template = Template->new({ INCLUDE_PATH => '/Users/7stud/perl_programs/cgi_projects/1proj') }); $template->process($file, $vars) or die "*MY* template process failed: ", $template->error(), "\n";
The url I'm using in my browser is:
http://localhost:8000/cgi-bin/create_form.pl
According the the Template Toolkit docs:
The process() method is called to process a template. The first parameter indicates the input template as one of: a filename relative to INCLUDE_PATH, if defined;.... docs
I am able to send a request to the program simple_cgi.pl, which does not use a template, and it works as expected. The url I use for that request is similar:
http://localhost:8000/cgi-bin/simple_cgi.pl
Here is simple_cgi.pl:
#!/usr/bin/env perl use strict; use warnings; use CGI; use CGI::Carp qw{ fatalsToBrowser }; my $cgi = CGI->new; print $cgi->header, $cgi->start_html, $cgi->div('hello'), $cgi->end_html;
What am I doing wrong?

Replies are listed 'Best First'.
Re: Template Toolkit: template paths, INCLUDE_PATH, error
by 7stud (Deacon) on Jan 06, 2015 at 11:25 UTC
    All that typing for nothing :(. After adding:
    use CGI; use CGI::Carp qw{ fatalsToBrowser };
    ...which I previously removed--thinking that might be a source of the error--I was notified of the error at the end of this line:
    INCLUDE_PATH => '/Users/7stud/perl_programs/cgi_projects/1proj')<---
    After removing the stray parenthesis, then I was notified of this error:
    my $tname = 'templates/form.html'; ... $template->process($file, $vars) #$tname <--> $file
Re: Template Toolkit: template paths, INCLUDE_PATH, error
by 7stud (Deacon) on Jan 06, 2015 at 11:37 UTC
    And now I can even get my INCLUDE blocks in my template to work:

    form.html:

    [% INCLUDE templates/header.html title = "Results page" %] <div>From template:</div> <div>[% message %]</div> [% INCLUDE templates/footer.html%]
    I hope that helps somebody else. One tip: don't forget the braces around the INCLUDE_PATH:
    my $template = Template->new({<----*****HERE INCLUDE_PATH => '/Users/7stud/perl_programs/cgi_projects/1proj' }); ^ | | *****AND HERE

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-29 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found