Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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; } } }

In reply to Re: CGI::Application Templates by rhesa
in thread CGI::Application Templates by logie17

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-19 00:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found