You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Todd Finney <tf...@boygenius.com> on 2000/08/23 16:28:22 UTC

executing a cgi from within a handler (templating redux)

Hi,

I'm building a simple templating system.   The major 
requirement of the system is that allow custom dynamic 
headers, footers, and toolbars based upon the identity of 
the user.

The system so far works like this:
         - a user enters the site and logs in.  The names 
of the user's default
         template, header, footer, and toolbar are placed 
into a cookie
         (via Apache::Session).
         - when the user requests a page, my handler 
intercepts that request, and
         looks at the user's cookie.  Based upon the 
information in it, it grabs the
         appropriate template and components from the 
filesystem along with the
         requested page, rolls them together, and serves 
the result.

A sample template file looks like this:

<html>
<head><title>Standard Template</title></head>
<body bgcolor="#ffffff">
<table width="650" cellspacing="0" cellpadding="0" 
border="0">
<tr><td colspan="2" width="650" align="left"><!-- 
Wrapper:header --><br></td></tr>
<tr>
  <td width="150" align="left" valign="top"><!-- 
Wrapper:toolbar --><br></td>
  <td width="500" align="left" valign="top"><!-- Content 
--><br></td></tr>
<tr><td colspan="2" width="650" align="left"><!-- 
Wrapper:footer --><br></td></tr>
</table>
</body></html>

Component files, such as the header, footer and toolbar, 
are by convention self-contained html tables, ala

# file components/tool/standard

<table width="100%">
<tr><td align="center"><a href="/wrapped/one.html">Link 
One</a><br></td></tr>
<tr><td align="center"><a href="/wrapped/two.html">Link 
Two</a><br></td></tr>
<tr><td align="center"><a href="/wrapped/three.html">Link 
Three</a><br></td></tr>
<tr><td align="center"><a href="/wrapped/four.html">Link 
Four</a><br></td></tr>
<tr><td align="center"><a href="/wrapped/five.html">Link 
Five</a><br></td></tr>
<tr><td align="center"><a 
href="/wrapped/cgi-bin/test.pl">CGI</a><br></td></tr></table>

I have a working version of this handler, but I think that 
there's a better way to do it, specifically the part that 
manages the content return part of the request.   Static 
files are simple enough; I open the file and print it to 
STDOUT.  Scripts, however, need to be handled 
differently.   The way I'm doing it now works, but it 
strikes me as inefficient.

package My::Wrapper;

use strict;
use Apache::Constants qw(:common DONE);
use Apache::Log ();

sub handler {
     my ( $r ) = shift;
     my ( $log ) = $r->log;
     $log->info("Wrapper: Inside Wrapper.");
     my ( $template_directory ) = "/www/html/templates/";
     my ( $components_directory ) = 
'/www/html/components/';
     #
     # these next four variables will come from the cookie, 
they are
     # set manually for now.
     #
     my ( $template ) = $template_directory.'standard';
     my ( $header ) = 
$components_directory.'head/'.'standard';
     my ( $toolbar ) = 
$components_directory.'tool/'.'standard';
     my ( $footer ) = 
$components_directory.'feet/'.'standard';
     $r->send_http_header;
     if ( -e $template ) {
         open( TEMPLATE, "$template" ) or die "Failed to 
open template $template: $!";
         while (<TEMPLATE>) {
             if ( $_ =~ 
/(.*)<\!--\sWrapper:(\w+)\s-->(.*)/o ) {
                 my ( $before ) = $1;
                 my ( $component ) = $2;
                 my ( $after ) = $3;
                 my ( $name );
                 $name = $header if $component eq 'header';
                 $name = $toolbar if $component eq 
'toolbar';
                 $name = $footer if $component eq 'footer';
                 print $before; &print_component($name); 
print $after;
             } elsif ( $_ =~ /(.*)<\!--\sContent\s-->(.*)/o 
) {
                 my ( $before ) = $1;
                 my ( $after ) = $2;
                 my ( $file ) = $r->filename;
                 print $before;
                 if ( -e $file ) {
                     if ( $file =~ /(?:cgi|pl)$/ ) {
                         $log->info("Wrapper: cgi script 
requested.");
                         do $file;
                     } else {
                         $log->info("Wrapper: static file 
requested.");
                         open(CONTENT, "$file" ) or die 
"Failed to open content file $file: $!";
                         while (<CONTENT>) {
                             print $_;
                         }
                         close(CONTENT);
                     }
                 }
                 print $after;
             } else {
                 print $_;
             }
         }
         close( TEMPLATE );
     }
     $log->info("Wrapper: Exiting Wrapper.");
     return DONE;
}

sub print_component {
     my ( $component ) = shift;
     if ( -e $component ) {
         open(IN, "$component" ) or die "Failed to open 
component $component: $!";
         while (<IN>) {
             print $_;
         }
         close(IN);
         return 1;
     } else {
         print "Failed to open component $component.";
         return 0;
     }
}
1;
__END__

I have experimented with various ways of handling this, 
such as printing a redirect, but this is the only way I 
could get it to work.  I suspect that there is a better way 
to do it.   I'd rather not `` the script, because of 
efficiency worries and the desire to maintain some process 
environment variables used in our authentication systems.

Thanks a bunch,
Todd




Re: executing a cgi from within a handler (templating redux)

Posted by Todd Finney <tf...@boygenius.com>.
At 03:00 PM 8/23/00, Matt Sergeant wrote:
>On Wed, 23 Aug 2000, Todd Finney wrote:
> > Almost, but we still have a couple of concerns about
> > it.  First, it would mean that we'd have to update all 
> the
> > cgis to not use CGI.pm for form argument handling.
>
>I doubt thats true - CGI.pm just gets its input from the 
>querystring or
>STDIN, so it should be able to intercept that in time, I 
>think.

Perhaps I misread the FAQ.

http://www.masonhq.com/docs/faq/#Should_I_use_CGI_pm_to_read_GET_


> >  Second,
> > we need to be able to do per-user templates, which I 
> don't
> > believe it can do (can AxKit do that?).
>
>Well I'm not sure exactly *how* per-user you want it. For 
>example is that
>1000 users and 1000 templates? But yes, it can certainly 
>do custom
>templates on a per-request basis. Thats a very basic 
>requirement. Just use
>named templates and a StyleChooser module of your making.

Cool.  Realistically, we need only 5-10 master templates 
from which to choose for each user.  I'll make sure we take 
a good look at AxKit if we ever get around to a change.

Thanks again.

cheers,
Todd


Re: executing a cgi from within a handler (templating redux)

Posted by Todd Finney <tf...@boygenius.com>.
At 02:11 PM 8/23/00, Perrin Harkins wrote:
>On Wed, 23 Aug 2000, Todd Finney wrote:
> > We were looking for a way to do only templating, and 
> leave
> > (essentially)  everything else on the site alone.
>
>For that, Template Toolkit, HTML::Template, or 
>CGI::FastTemplate are
>probably your best options.

As I said, Template::Toolkit is nice, but more tool than we 
need.  I'll have to investigate the last two.

> > Almost, but we still have a couple of concerns about
> > it.  First, it would mean that we'd have to update all 
> the
> > cgis to not use CGI.pm for form argument handling.
>
>You mean for HTML generation and "sticky" forms?  You can 
>still use CGI.pm
>for that with Template Toolkit and I think you can with 
>Mason as
>well.  Take a look at the mail archives.

The CGI.pm concern was specifically related to Mason.  See 
my reply to Matt for a link to the FAQ to which I was 
referring.

> > Second, we need to be able to do per-user templates, 
> which I don't
> > believe it can do (can AxKit do that?).
>
>You can modify the template path at run time with any of 
>these, which
>should do the trick.

I didn't realize that was so basic a requirement, but I 
suppose it would be handy in a lot of situations.

thanks,
Todd



Re: executing a cgi from within a handler (templating redux)

Posted by Perrin Harkins <pe...@primenet.com>.
On Wed, 23 Aug 2000, Todd Finney wrote:
> We were looking for a way to do only templating, and leave
> (essentially)  everything else on the site alone.

For that, Template Toolkit, HTML::Template, or CGI::FastTemplate are
probably your best options.

> Almost, but we still have a couple of concerns about 
> it.  First, it would mean that we'd have to update all the 
> cgis to not use CGI.pm for form argument handling.

You mean for HTML generation and "sticky" forms?  You can still use CGI.pm
for that with Template Toolkit and I think you can with Mason as
well.  Take a look at the mail archives.

> Second, we need to be able to do per-user templates, which I don't
> believe it can do (can AxKit do that?).

You can modify the template path at run time with any of these, which
should do the trick.

- Perrin


Re: executing a cgi from within a handler (templating redux)

Posted by Matt Sergeant <ma...@sergeant.org>.
On Wed, 23 Aug 2000, Todd Finney wrote:

> Managing the site in a different way would be pretty great, 
> but we've been asked to do something fairly complicated 
> (this borders thingy) on very short time.  We were looking 
> for a way to do only templating, and leave (essentially) 
> everything else on the site alone.   There's interest in 
> moving to a different system at some point in the future, 
> but we just can't get it done now.

AxKit makes site development really quick, but only if you understand "The
XML Way"... So I totally see where you're coming from.

> >I also think Mason can do what you're after.
> 
> Almost, but we still have a couple of concerns about 
> it.  First, it would mean that we'd have to update all the 
> cgis to not use CGI.pm for form argument handling.

I doubt thats true - CGI.pm just gets its input from the querystring or
STDIN, so it should be able to intercept that in time, I think.

>  Second, 
> we need to be able to do per-user templates, which I don't 
> believe it can do (can AxKit do that?).

Well I'm not sure exactly *how* per-user you want it. For example is that
1000 users and 1000 templates? But yes, it can certainly do custom
templates on a per-request basis. Thats a very basic requirement. Just use
named templates and a StyleChooser module of your making.

-- 
<Matt/>

Fastnet Software Ltd. High Performance Web Specialists
Providing mod_perl, XML, Sybase and Oracle solutions
Email for training and consultancy availability.
http://sergeant.org | AxKit: http://axkit.org


Re: executing a cgi from within a handler (templating redux)

Posted by Todd Finney <tf...@boygenius.com>.
At 12:03 PM 8/23/00, Matt Sergeant wrote:
>On Wed, 23 Aug 2000, Todd Finney wrote:
> > We looked at Template::Toolkit and also at Axkit, but 
> both
> > seemed to be much larger hammers than we needed.   It 
> also
> > (and I could be incorrect here) did not appear to have 
> the
> > capability to select the included component dynamically 
>
> > based upon information provided at request time, and 
> pass
> > that information into the component.
>
>AxKit does, but I accept the thing about it being a big 
>hammer for a
>potentially small job. You should use something like AxKit 
>when you're
>looking to build a site that will change at some point, 
>and you didn't
>like managing your site "the old way".

Thanks, Matt.

Managing the site in a different way would be pretty great, 
but we've been asked to do something fairly complicated 
(this borders thingy) on very short time.  We were looking 
for a way to do only templating, and leave (essentially) 
everything else on the site alone.   There's interest in 
moving to a different system at some point in the future, 
but we just can't get it done now.

>I also think Mason can do what you're after.

Almost, but we still have a couple of concerns about 
it.  First, it would mean that we'd have to update all the 
cgis to not use CGI.pm for form argument handling.  Second, 
we need to be able to do per-user templates, which I don't 
believe it can do (can AxKit do that?).

I've never used Mason, this is just what I got from the FAQ 
and the doc.   If there's a way around this, we'd take 
another look at it.

Todd
  


Re: executing a cgi from within a handler (templating redux)

Posted by Matt Sergeant <ma...@sergeant.org>.
On Wed, 23 Aug 2000, Todd Finney wrote:

> Thanks, Ken.
> 
> We looked at Template::Toolkit and also at Axkit, but both 
> seemed to be much larger hammers than we needed.   It also 
> (and I could be incorrect here) did not appear to have the 
> capability to select the included component dynamically 
> based upon information provided at request time, and pass 
> that information into the component.

AxKit does, but I accept the thing about it being a big hammer for a
potentially small job. You should use something like AxKit when you're
looking to build a site that will change at some point, and you didn't
like managing your site "the old way".

I also think Mason can do what you're after.

-- 
<Matt/>

Fastnet Software Ltd. High Performance Web Specialists
Providing mod_perl, XML, Sybase and Oracle solutions
Email for training and consultancy availability.
http://sergeant.org | AxKit: http://axkit.org


Re: executing a cgi from within a handler (templating redux)

Posted by Todd Finney <tf...@boygenius.com>.
Thanks, Ken.

We looked at Template::Toolkit and also at Axkit, but both 
seemed to be much larger hammers than we needed.   It also 
(and I could be incorrect here) did not appear to have the 
capability to select the included component dynamically 
based upon information provided at request time, and pass 
that information into the component.

There was also a concern with implementation time.  The 
site in question is already operational, with a few hundred 
static pages and a fair number of CGIs.   Retrofitting all 
of the pages to work with the toolkit seemed to be a 
daunting task.  That's not even including the time involved 
in teaching your web developers how to use the new system.

My plan seems simpler - create the default header, footer, 
and toolbar files, write a script to strip out that 
information from the existing site files, turn on the 
handler, and go.   Don't let the templating system handle 
anything except templating, and leave all the logic up to 
the component and content files which could be html, cgi, 
anything.

Todd





At 10:41 AM 8/23/00, Ken Y. Clark wrote:
>is there a particular reason why you've not chosen to use 
>one of the very
>excellent existing templating systems?  i've used 
>HTML::Template for many
>things;  it's very lightweight and incredibly easy, and 
>i'm pretty sure it
>would solve your problems.  lately, i've been getting into
>Template::Toolkit, and i find it to be *very* powerful, 
>easy, and much
>more flexible than HTML::Template.  i'd really recommend 
>you exhaust all
>your options with one of the currently available template 
>kits on CPAN
>before you roll your own.
>
>ky


Re: executing a cgi from within a handler (templating redux)

Posted by Todd Finney <tf...@boygenius.com>.
At 12:22 PM 8/23/00, Alex Menendez wrote:

>Hmmmm, the do does seem a little inefficient. I solved 
>this problem in the
>past by intiating a subrequest and changing the stack 
>handler to
>cgi-script right before running the cgi. something like 
>this:
>
>$subr = $r->lookup_uri($uri);
>if($r->filename =~ /\.(cgi|pl)$)/o) {
>     $subr->handler('cgi-script');
>}
>$subr->run();

Thanks, Alex.

This will work, but I'm trying to eliminate the 'what kind 
of file is this' logic from the handler.   Perhaps I'm 
being naive, but if I can just give Apache a generic 
request and let Apache worry about whether it's a static 
file or a script, the problem gets a lot simpler.

Perhaps I'm seeing a performance problem where there isn't 
one.  Do you think that the type determination code is 
going to slow anything down measurably?

>you might get into some trouble with headers if your cgi's 
>return
>http headers other than just Content-type like Location. 
>If they don't go
>ahead and add $r->send_http_header right before the run 
>command. If they
>do send other headers, you will need a patch for Apache.xs 
>in the mod_perl
>src. I have it and can give it to you if you are 
>interested.

Sure, I'd like to take a look at it.

thanks again,
Todd


On Wed, 23 Aug 2000, Todd Finney wrote:

> > Hi,
> >
> > I'm building a simple templating system.   The major
> > requirement of the system is that allow custom dynamic
> > headers, footers, and toolbars based upon the identity 
> of
> > the user.
> >
> > The system so far works like this:
> >          - a user enters the site and logs in.  The 
> names
> > of the user's default
> >          template, header, footer, and toolbar are 
> placed
> > into a cookie
> >          (via Apache::Session).
> >          - when the user requests a page, my handler
> > intercepts that request, and
> >          looks at the user's cookie.  Based upon the
> > information in it, it grabs the
> >          appropriate template and components from the
> > filesystem along with the
> >          requested page, rolls them together, and 
> serves
> > the result.
> >
> > A sample template file looks like this:
> >
> > <html>
> > <head><title>Standard Template</title></head>
> > <body bgcolor="#ffffff">
> > <table width="650" cellspacing="0" cellpadding="0"
> > border="0">
> > <tr><td colspan="2" width="650" align="left"><!--
> > Wrapper:header --><br></td></tr>
> > <tr>
> >   <td width="150" align="left" valign="top"><!--
> > Wrapper:toolbar --><br></td>
> >   <td width="500" align="left" valign="top"><!-- 
> Content
> > --><br></td></tr>
> > <tr><td colspan="2" width="650" align="left"><!--
> > Wrapper:footer --><br></td></tr>
> > </table>
> > </body></html>
> >
> > Component files, such as the header, footer and 
> toolbar,
> > are by convention self-contained html tables, ala
> >
> > # file components/tool/standard
> >
> > <table width="100%">
> > <tr><td align="center"><a href="/wrapped/one.html">Link 
>
> > One</a><br></td></tr>
> > <tr><td align="center"><a href="/wrapped/two.html">Link 
>
> > Two</a><br></td></tr>
> > <tr><td align="center"><a 
> href="/wrapped/three.html">Link
> > Three</a><br></td></tr>
> > <tr><td align="center"><a 
> href="/wrapped/four.html">Link
> > Four</a><br></td></tr>
> > <tr><td align="center"><a 
> href="/wrapped/five.html">Link
> > Five</a><br></td></tr>
> > <tr><td align="center"><a
> > 
> href="/wrapped/cgi-bin/test.pl">CGI</a><br></td></tr></table>
> >
> > I have a working version of this handler, but I think 
> that
> > there's a better way to do it, specifically the part 
> that
> > manages the content return part of the 
> request.   Static
> > files are simple enough; I open the file and print it 
> to
> > STDOUT.  Scripts, however, need to be handled
> > differently.   The way I'm doing it now works, but it
> > strikes me as inefficient.
> >
> > package My::Wrapper;
> >
> > use strict;
> > use Apache::Constants qw(:common DONE);
> > use Apache::Log ();
> >
> > sub handler {
> >      my ( $r ) = shift;
> >      my ( $log ) = $r->log;
> >      $log->info("Wrapper: Inside Wrapper.");
> >      my ( $template_directory ) = 
> "/www/html/templates/";
> >      my ( $components_directory ) =
> > '/www/html/components/';
> >      #
> >      # these next four variables will come from the 
> cookie,
> > they are
> >      # set manually for now.
> >      #
> >      my ( $template ) = $template_directory.'standard';
> >      my ( $header ) =
> > $components_directory.'head/'.'standard';
> >      my ( $toolbar ) =
> > $components_directory.'tool/'.'standard';
> >      my ( $footer ) =
> > $components_directory.'feet/'.'standard';
> >      $r->send_http_header;
> >      if ( -e $template ) {
> >          open( TEMPLATE, "$template" ) or die "Failed 
> to
> > open template $template: $!";
> >          while (<TEMPLATE>) {
> >              if ( $_ =~
> > /(.*)<\!--\sWrapper:(\w+)\s-->(.*)/o ) {
> >                  my ( $before ) = $1;
> >                  my ( $component ) = $2;
> >                  my ( $after ) = $3;
> >                  my ( $name );
> >                  $name = $header if $component eq 
> 'header';
> >                  $name = $toolbar if $component eq
> > 'toolbar';
> >                  $name = $footer if $component eq 
> 'footer';
> >                  print $before; 
> &print_component($name);
> > print $after;
> >              } elsif ( $_ =~ 
> /(.*)<\!--\sContent\s-->(.*)/o
> > ) {
> >                  my ( $before ) = $1;
> >                  my ( $after ) = $2;
> >                  my ( $file ) = $r->filename;
> >                  print $before;
> >                  if ( -e $file ) {
> >                      if ( $file =~ /(?:cgi|pl)$/ ) {
> >                          $log->info("Wrapper: cgi 
> script
> > requested.");
> >                          do $file;
> >                      } else {
> >                          $log->info("Wrapper: static 
> file
> > requested.");
> >                          open(CONTENT, "$file" ) or die 
>
> > "Failed to open content file $file: $!";
> >                          while (<CONTENT>) {
> >                              print $_;
> >                          }
> >                          close(CONTENT);
> >                      }
> >                  }
> >                  print $after;
> >              } else {
> >                  print $_;
> >              }
> >          }
> >          close( TEMPLATE );
> >      }
> >      $log->info("Wrapper: Exiting Wrapper.");
> >      return DONE;
> > }
> >
> > sub print_component {
> >      my ( $component ) = shift;
> >      if ( -e $component ) {
> >          open(IN, "$component" ) or die "Failed to open 
>
> > component $component: $!";
> >          while (<IN>) {
> >              print $_;
> >          }
> >          close(IN);
> >          return 1;
> >      } else {
> >          print "Failed to open component $component.";
> >          return 0;
> >      }
> > }
> > 1;
> > __END__
> >
> > I have experimented with various ways of handling this, 
>
> > such as printing a redirect, but this is the only way I 
>
> > could get it to work.  I suspect that there is a better 
> way
> > to do it.   I'd rather not `` the script, because of
> > efficiency worries and the desire to maintain some 
> process
> > environment variables used in our authentication 
> systems.
> >
> > Thanks a bunch,
> > Todd
> >
> >
> >


Re: executing a cgi from within a handler (templating redux)

Posted by Alex Menendez <am...@hotbacon.com>.
Hmmmm, the do does seem a little inefficient. I solved this problem in the
past by intiating a subrequest and changing the stack handler to
cgi-script right before running the cgi. something like this:

$subr = $r->lookup_uri($uri);
if($r->filename =~ /\.(cgi|pl)$)/o) {
    $subr->handler('cgi-script');
}
$subr->run();

you might get into some trouble with headers if your cgi's return
http headers other than just Content-type like Location. If they don't go 
ahead and add $r->send_http_header right before the run command. If they
do send other headers, you will need a patch for Apache.xs in the mod_perl
src. I have it and can give it to you if you are interested.

-amen

On Wed, 23 Aug 2000, Todd Finney wrote:

> Hi,
> 
> I'm building a simple templating system.   The major 
> requirement of the system is that allow custom dynamic 
> headers, footers, and toolbars based upon the identity of 
> the user.
> 
> The system so far works like this:
>          - a user enters the site and logs in.  The names 
> of the user's default
>          template, header, footer, and toolbar are placed 
> into a cookie
>          (via Apache::Session).
>          - when the user requests a page, my handler 
> intercepts that request, and
>          looks at the user's cookie.  Based upon the 
> information in it, it grabs the
>          appropriate template and components from the 
> filesystem along with the
>          requested page, rolls them together, and serves 
> the result.
> 
> A sample template file looks like this:
> 
> <html>
> <head><title>Standard Template</title></head>
> <body bgcolor="#ffffff">
> <table width="650" cellspacing="0" cellpadding="0" 
> border="0">
> <tr><td colspan="2" width="650" align="left"><!-- 
> Wrapper:header --><br></td></tr>
> <tr>
>   <td width="150" align="left" valign="top"><!-- 
> Wrapper:toolbar --><br></td>
>   <td width="500" align="left" valign="top"><!-- Content 
> --><br></td></tr>
> <tr><td colspan="2" width="650" align="left"><!-- 
> Wrapper:footer --><br></td></tr>
> </table>
> </body></html>
> 
> Component files, such as the header, footer and toolbar, 
> are by convention self-contained html tables, ala
> 
> # file components/tool/standard
> 
> <table width="100%">
> <tr><td align="center"><a href="/wrapped/one.html">Link 
> One</a><br></td></tr>
> <tr><td align="center"><a href="/wrapped/two.html">Link 
> Two</a><br></td></tr>
> <tr><td align="center"><a href="/wrapped/three.html">Link 
> Three</a><br></td></tr>
> <tr><td align="center"><a href="/wrapped/four.html">Link 
> Four</a><br></td></tr>
> <tr><td align="center"><a href="/wrapped/five.html">Link 
> Five</a><br></td></tr>
> <tr><td align="center"><a 
> href="/wrapped/cgi-bin/test.pl">CGI</a><br></td></tr></table>
> 
> I have a working version of this handler, but I think that 
> there's a better way to do it, specifically the part that 
> manages the content return part of the request.   Static 
> files are simple enough; I open the file and print it to 
> STDOUT.  Scripts, however, need to be handled 
> differently.   The way I'm doing it now works, but it 
> strikes me as inefficient.
> 
> package My::Wrapper;
> 
> use strict;
> use Apache::Constants qw(:common DONE);
> use Apache::Log ();
> 
> sub handler {
>      my ( $r ) = shift;
>      my ( $log ) = $r->log;
>      $log->info("Wrapper: Inside Wrapper.");
>      my ( $template_directory ) = "/www/html/templates/";
>      my ( $components_directory ) = 
> '/www/html/components/';
>      #
>      # these next four variables will come from the cookie, 
> they are
>      # set manually for now.
>      #
>      my ( $template ) = $template_directory.'standard';
>      my ( $header ) = 
> $components_directory.'head/'.'standard';
>      my ( $toolbar ) = 
> $components_directory.'tool/'.'standard';
>      my ( $footer ) = 
> $components_directory.'feet/'.'standard';
>      $r->send_http_header;
>      if ( -e $template ) {
>          open( TEMPLATE, "$template" ) or die "Failed to 
> open template $template: $!";
>          while (<TEMPLATE>) {
>              if ( $_ =~ 
> /(.*)<\!--\sWrapper:(\w+)\s-->(.*)/o ) {
>                  my ( $before ) = $1;
>                  my ( $component ) = $2;
>                  my ( $after ) = $3;
>                  my ( $name );
>                  $name = $header if $component eq 'header';
>                  $name = $toolbar if $component eq 
> 'toolbar';
>                  $name = $footer if $component eq 'footer';
>                  print $before; &print_component($name); 
> print $after;
>              } elsif ( $_ =~ /(.*)<\!--\sContent\s-->(.*)/o 
> ) {
>                  my ( $before ) = $1;
>                  my ( $after ) = $2;
>                  my ( $file ) = $r->filename;
>                  print $before;
>                  if ( -e $file ) {
>                      if ( $file =~ /(?:cgi|pl)$/ ) {
>                          $log->info("Wrapper: cgi script 
> requested.");
>                          do $file;
>                      } else {
>                          $log->info("Wrapper: static file 
> requested.");
>                          open(CONTENT, "$file" ) or die 
> "Failed to open content file $file: $!";
>                          while (<CONTENT>) {
>                              print $_;
>                          }
>                          close(CONTENT);
>                      }
>                  }
>                  print $after;
>              } else {
>                  print $_;
>              }
>          }
>          close( TEMPLATE );
>      }
>      $log->info("Wrapper: Exiting Wrapper.");
>      return DONE;
> }
> 
> sub print_component {
>      my ( $component ) = shift;
>      if ( -e $component ) {
>          open(IN, "$component" ) or die "Failed to open 
> component $component: $!";
>          while (<IN>) {
>              print $_;
>          }
>          close(IN);
>          return 1;
>      } else {
>          print "Failed to open component $component.";
>          return 0;
>      }
> }
> 1;
> __END__
> 
> I have experimented with various ways of handling this, 
> such as printing a redirect, but this is the only way I 
> could get it to work.  I suspect that there is a better way 
> to do it.   I'd rather not `` the script, because of 
> efficiency worries and the desire to maintain some process 
> environment variables used in our authentication systems.
> 
> Thanks a bunch,
> Todd
> 
> 
> 


Re: executing a cgi from within a handler (templating redux)

Posted by "Ken Y. Clark" <kc...@boston.com>.
On Wed, 23 Aug 2000, Todd Finney wrote:

> Hi,
> 
> I'm building a simple templating system.   The major 
> requirement of the system is that allow custom dynamic 
> headers, footers, and toolbars based upon the identity of 
> the user.
> 

is there a particular reason why you've not chosen to use one of the very
excellent existing templating systems?  i've used HTML::Template for many
things;  it's very lightweight and incredibly easy, and i'm pretty sure it
would solve your problems.  lately, i've been getting into
Template::Toolkit, and i find it to be *very* powerful, easy, and much
more flexible than HTML::Template.  i'd really recommend you exhaust all
your options with one of the currently available template kits on CPAN
before you roll your own.

ky