Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

CGI::Application Templates

by logie17 (Friar)
on Feb 06, 2007 at 06:19 UTC ( #598490=perlquestion: print w/replies, xml ) Need Help??

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

I'm using CGI::Application for a site I'm trying to intelligently design. I have a master template file that will be serving two purposes: 1 display output of a given run mode and 2 display a navigation tree. I load my template path in the cgiapp_prerun and I have the following example run_mode:
sub examplRM{ my $self = shift; my $template = $self->load_tmpl('master.tmpl'); my $output = 'example' $template->param( outputVar => $output); return $template->output;

Now I have another method that loads a navigation bar:
sub exampleNav{ my $self = shift; my @nav = qw(Nav1, Nav2, Nav3); return \@nav; }
What I would like to do is preload examplNav into my template object. So I don't have to recall the navigation method for additional run methods. Anybody have any suggestions?

Thanks in adavance!
s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

Replies are listed 'Best First'.
Re: CGI::Application Templates
by rhesa (Vicar) on Feb 06, 2007 at 12:55 UTC
    First of all, there are the generic HTML::Template options (using a <tmpl_include>, for instance). See the recent article on Perl.com, Advanced HTML::Template: Widgets.

    CGI::Application offers several ways to help implement this. My first choice would probably be the load_tmpl hook. It would allow you to set the required variables whenever you load your main template:

    sub navbar_callback { my ($self, $ht_params, $tmpl_params, $tmpl_file) = @_; if( $tmpl_file eq 'master.tmpl' ) { # load your variables my %navbar_params = $self->navbar_params; $tmpl_params->{$_} = $navbar_params{$_} foreach keys %navbar_p +arams; # alternatively, you could also load a separate template for t +he navbar, and # pass it on to the master as a tmpl_var. Note that the if() c +lause protects you # from an infinite loop # $tmpl_params->{navbar} = $self->navbar_template; } }
    You turn this callback on in e.g. cgiapp_init or setup:
    sub setup { my $self = shift; $self->add_callback( 'load_tmpl' => 'navbar_callback' ); }
    Now every time you call $self->load_tmpl('master.tmpl'), the navbar will be filled automatically.

    Another approach I used before the callback infrastructure was available, was to use the cgiapp_postrun method to fill in the bits and pieces: every run mode would only return the output specific to it, while the cgiapp_postrun would stitch the pieces together into the overall page. I did this by loading the master.tmpl, passing it the runmode output as a var, and loading other generic stuff as well:

    sub cgiapp_postrun { my ( $self, $outputref ) = @_; # the following conditionals protect us from trampling on # 'redirect' runmodes, or non-html output. if( $self->header_type eq 'header' ) { my %props = $self->header_props; if( !exists( $props{'-type'} ) or $props{'-type'} eq 'text/htm +l' ) { my $t = $self->load_tmpl('master.tmpl'); $t->param( runmode_content => $$outputref ); $t->param( navbar_content => $self->navbar_template ); $$outputref = $t->output; } } }
      Thank you, that was some very helpful advice and exactly what I needed.

      Thanks again
      s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2022-08-15 13:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?