http://qs321.pair.com?node_id=320501

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

I've started learning CGI::Application with the new job I started, and I've gotten to a point where I'm stuck.

I know that the default templating module is HTML::Template. Even though I prefer Template::Toolkit for this sort of thing, using HTML::Template is a requirement I can't change. ( The company says it's "too complex,", "too powerful," and "too confusing for an end user when we want them to make changed to their HTML." ) I saw "BAH", but that's not the point.

Without hacking the source of CGI::Application, how do I pass HTML::Template options (specifically loop_context_vars) into an instance of CGI::Application so that it gets passed onto HTML::Template?

Normally I would just trial-and-error the code until it worked, but I'm replacing someone who went abroad to work and I've been dropped into the situation and need to get things done *superfast* because the owner of the company is trying to minimise the rampup time as much as possible. (A source of minor frustration because nothing is documented at this place.)

Replies are listed 'Best First'.
Re: CGI::Application and HTML::Template interaction
by archon (Monk) on Jan 11, 2004 at 19:14 UTC
    from the man page, i believe it would be like this:
    my $tmpl_obj = $webapp->load_tmpl('some_other.tmpl', loop_context_vars => 1, die_on_bad_params => 0, cache => 1 );
      thanks. as i mentioned in my other reply, i expected it to be in the constructor, not an override in one method call ...
Re: CGI::Application and HTML::Template interaction
by thraxil (Prior) on Jan 11, 2004 at 19:17 UTC
      thanks. i'd read through the perldoc too quickly and missed that part. i was expecting it to be in the constructor for CGI::Application for some reason.
Re: CGI::Application and HTML::Template interaction
by cees (Curate) on Jan 11, 2004 at 22:07 UTC

    You already have the correct answer above, but there are other ways of dealing with this if you get tired of always having to pass those parameters when you call load_tmpl.

    If all your templates need the loop_context_vars parameter set, then I would override the load_tmpl method to always provide it for you.

    ################# # load_tmpl # ################# # # Over ride the CGI::Application load_tmpl method so that # we can automatically add our own parameters to all # templates # sub load_tmpl { my $self = shift; my $template_name = shift; my %options = @_; # Turn off 'die_on_bad_params' unless it has been explicitely set $options{die_on_bad_params} = 0 unless defined $options{die_on_bad_params}; # Turn on 'loop_context_vars' unless it's been explicitely disabled $options{loop_context_vars} = 1 unless defined $options{loop_context_vars}; # Load the template using the real CGI::Application load_tmpl method my $template = $self->SUPER::load_tmpl($template_name, %options); return $template; }

    - Cees

      thanks, but that's my last resort, as all the other in-house apps use CGI::Application and i don't want to go making global 'company' changes without knowing the repercussions.

        You don't have to be that drastic with the change... You can add this function your any of your CGI::Application subclasses and have it work for you. You don't have to edit the CGI::Application source to add this.

        To use CGI::Application you need to write a module that inherits from CGI::Application (by adding 'use base CGI::Application;' to the top of your module). You can add the custom load_tmpl function to this module (right along side your runmodes) and it will work properly.

        The way I usually do it (and the way most CGI::Application users recommend) is to create a project wide Base class that inherits from CGI::Application, where you can customize the functionality of CGI::Application for the entire project (adding session management, authentication, etc...). Then you can create several modules with runmodes that inherit from your Base class. The Base class is where you would ideally put the customized load_tmpl method. This way your entire project gets the benefit of the change, but other users of CGI::Application on the system will not notice a difference.

        If you would like a detailed example of how this is implemented, let me know and I'll put something together for you.

        - Cheers