in reply to CGI::Application same function code
I've written a few applications which just display a template file, and the simplest way I found was to use the AUTOLOAD function.
That gets called for anything that hasn't got an explicit mapping setup. Inside that I will either show "Invalid node", or laod the existing template.
Code could look like this:
# # Called if the user submits an invalid run mode / node. # sub invalidnode { my ($self) = @_; # Get the node the user tried to use. my $q = $self->query(); my $node = $q->param("node"); if ( $node =~ /^([a-z]+)$/ ) { $node = $1; if ( -e "./templates/$node.tmpl" ) { my $html = $self->load_tmpl( "templates/$node.tmpl", global_vars => 1, ); return ( $html->output ); } } my $html = $self->load_tmpl( "templates/error.tmpl", global_vars => 1, ); return ( $html->output ); }
With at the setup:
$self->run_modes( 'home' => 'home', ..... 'AUTOLOAD' => 'invalidnode' );
As you see I merely test for the existance of a template - and if one exists it's loaded, if not I show an error page.
That means I can have ?node=FAQ, ?node=Contact, etc, which are just static templates and don't need any special handling.
|
|---|