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;
}
}
}
|