Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Template simple with CGI::Application

by Anonymous Monk
on Jan 08, 2013 at 18:14 UTC ( #1012300=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I am trying to get some understanding on this script using CGI::Application but using
a different template system them HTML::Template. I am trying to use Template::Simple. I can't
figure it out how would the changes be, if someone here has or had any previous work
on this kind of thing and would like to tell me how I can do this, it would be very nice!
Here is some sample code I am trying with:
# package Hello; use lib qw(.); use base 'CGI::Application'; use lib '../lib' ; use Template::Simple; use strict; # Run at startup sub setup { my $self = shift; $self->start_mode('mode1'); $self->run_modes( 'mode1' => 'helloworld_sub', ); # Use current path as template path, # i.e. the template is in the same directory as this script #$self->tmpl_path('./'); $self->Template::Simple->new( search_dirs => [ ('./') ], ); } # Usgin Template Simple sub helloworld_sub { # The application object my $self = shift; # The message my $that_famous_string = 'Hello, there World!'; # Open the html template # NOTE 1: load_tmpl() is a CGI::Application method, not a HTML::Te +mplate method. # NOTE 2: Setting the 'cache' flag caches the template if used wit +h mod_perl. #my $template = $self->load_tmpl( # 'hello.tmpl', # cache => 1, # ); # Fill in some parameters #$template->param( # THAT_FAMOUS_STRING => $that_famous_string, # ); # Parse the template #my $html_output = $template->output; ## Using Template Simple my $display_vars; $display_vars->{'famous'} = $that_famous_string; $self->compile( 'hello'); my $html_output = $self->render( 'hello', $display_vars ); #print $$html_output; return $html_output; } 1;

this is the template code:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN"> <html> <head> <title>[% famous %]</title> </head> <body> <h3>Frame Test</h3></b> <b>[% famous %]</b> </body> </html>

The caller code:
#!/usr/bin/perl -wT ## ## CGI Application Hello World ## use strict; use lib qw(.); use Hello; my $helloworld = Hello->new(); $helloworld->run();

Thanks for looking!

Replies are listed 'Best First'.
Re: Template simple with CGI::Application
by sundialsvc4 (Abbot) on Jan 09, 2013 at 01:43 UTC

    If you have in your possession “code that works” that uses one templating-system successfully, I for one would be very reluctant to attempt to change it to use something else.   The logic which supports the templated displays is usually deeply rooted in how that particular templating system works.   And, I simply do not see the justification in not doing what the Romans do.   Please elaborate as to why you find this to be necessary ...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012300]
Approved by blue_cowdawg
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-06-24 21:44 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.