in reply to CGI::Application Templates
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:
You turn this callback on in e.g. cgiapp_init or setup: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; } }
Now every time you call $self->load_tmpl('master.tmpl'), the navbar will be filled automatically.sub setup { my $self = shift; $self->add_callback( 'load_tmpl' => 'navbar_callback' ); }
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; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: CGI::Application Templates
by logie17 (Friar) on Feb 06, 2007 at 16:02 UTC |