Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: CGI::Application Templates

by rhesa (Vicar)
on Feb 06, 2007 at 12:55 UTC ( [id://598520]=note: print w/replies, xml ) Need Help??


in reply to CGI::Application Templates

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

Replies are listed 'Best First'.
Re^2: CGI::Application Templates
by logie17 (Friar) on Feb 06, 2007 at 16:02 UTC
    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: note [id://598520]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-16 23:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found