in reply to Re^2: Multiple Pages with CGI
in thread Multiple Pages with CGI
Some of the perl web-application frameworks do use a mapping from apache handlers to perl subs... or with some other sort of callback magic. I think maypole does that, CGI::Application also does.
If you want a request to call a sub, you just need a dispatch table keyed by page name, or id
#use strict unless untested_code; use Template; use CGI qw/header/; print header; # hash of subs that return 2 element list of page title and page conte +nt. my %title_and_content_for = ( # about_us => \&MyModule::get_about_us_html, '/quick_hack' => sub { return ( 'this is a quick, nasty hack', 'some junk to go in the page' ) }, # ... ); my %template_variables = ( # the default is to complain, this will be replaced below title => 'Oh noes!', content => q[ I couldn't find the page you asked for ], # add other stuff to keys in %template_variables ); my $html_layout = q{ <html> <head><title>[% title %]</title></head> <body> [% content %] </body> </html> }; # call the sub for this page, if there is one. # assign the results of the sub to the keys "title" and "content" @template_variables{title,content} = $title_and_content_for{ $ENV{ PATH_INFO } }->() if defined $title_and_content_for{ $ENV{ PATH_INFO } }; # and feed the whole lot to Template toolkit my $TT = Template->new() or die $TT->error(); $TT->process(\$html_layout,\%template_variables) or die $TT->error();
Now we view http://mysite.com/cgi-bin/sub-madness.cgi/quick_hack and if all goes according to plan (and my untested-make-it-up-on-the-spottery is as good as I think it is) you'll get a nice little html page that reads "some junk to go in the page".
I've cheated a little, and stuffed most of the HTML generation into places you can't see, the subs on one hand, and Template::Toolkit on the other
Other interesting this to do:
Update
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Multiple Pages with CGI
by gloryhack (Deacon) on Mar 26, 2007 at 16:34 UTC | |
by f00li5h (Chaplain) on Mar 27, 2007 at 03:36 UTC | |
by gloryhack (Deacon) on Mar 27, 2007 at 04:35 UTC |