Apache-SimpleReplace

 view release on metacpan or  search on metacpan

SimpleReplace.pm  view on Meta::CPAN

    return SERVER_ERROR;
  }

  my ($rqh, $status);

  # open the request handle
  if ($filter) {
    $log->info("\tgetting input from Apache::Filter")
      if $Apache::SimpleReplace::DEBUG;
    ($rqh, $status) = $r->filter_input;
    undef $rqh unless $status == OK;     # just to be sure...
  } else {
    $log->info("\tgetting input from requested file")
      if $Apache::SimpleReplace::DEBUG;
    $rqh = Apache::File->new($r->filename);
  }

  unless ($rqh) {
    $log->error("\tcannot open request! $!");
    $log->info("Exiting Apache::SimpleReplace");
    return SERVER_ERROR;
  }

  $r->send_http_header('text/html');

  # send output
  while (<$tph>) {
    if (/\Q$replace/) {
      $log->info("\t\'$replace\' found - replacing with request") 
        if $Apache::SimpleReplace::DEBUG;  

      my ($left, $right) = split /\Q$replace/;
  
      print $left;            # output the left side of substitution

      $r->send_fd($rqh);      # Apache::Filter > 1.013 overrides
                              # send_fd()

      print $right;           # ouptut the right side of substitution
    }
    else {
      print;                  # print each template line
    }
  }
 
#---------------------------------------------------------------------
# wrap up...
#---------------------------------------------------------------------

   $log->info("Exiting Apache::SimpleReplace");

   return OK;
}

1;
 
__END__

=head1 NAME 

Apache::SimpleReplace - a simple template framework

=head1 SYNOPSIS

httpd.conf:

 <Location /someplace>
    SetHandler perl-script
    PerlHandler Apache::SimpleReplace

    PerlSetVar  TEMPLATE "/templates/format1.html"
    PerlSetVar  REPLACE "the content goes here"
 </Location>  

Apache::SimpleReplace is Filter aware, meaning that it can be used
within an Apache::Filter framework without modification.  Just
include the directive
  
  PerlSetVar Filter On

and modify the PerlHandler directive accordingly.  As of version
0.06, Apache::SimpleReplace requires Apache::Filter 1.013 or
better - users of Apache::Filter 1.011 or less should use version
0.05.

=head1 DESCRIPTION

Apache::SimpleReplace provides a simple way to insert content within
an established template for uniform content delivery.  While the end
result is similar to Apache::Sandwich, Apache::SimpleReplace offers
several advantages.

  o It does not use separate header and footer files, easing the
    pain of maintaining syntactically correct HTML in seperate files.

  o It is Apache::Filter aware, thus it can both accept content from
    other content handlers as well as pass its changes on to others
    later in the chain.

=head1 EXAMPLE

/usr/local/apache/templates/format1.html:

  <html>
      <head><title>your template</title></head>
              <title>your template</title>
      <body bgcolor="#778899">
              some headers, banners, whatever...
              <p>
  the content goes here
              </p>
              <p>some footers, modification dates, whatever...
      </body>
  </html> 

 httpd.conf:

  <Location /someplace>
     SetHandler perl-script
     PerlHandler Apache::SimpleReplace Apache::SSI

     PerlSetVar  TEMPLATE "templates/format1.html"
     PerlSetVar  REPLACE "the content goes here"
     PerlSetVar  Filter On
  </Location>

Now, a request to http://localhost/someplace/foo.html will insert
the contents of foo.html in place of "the content goes here" in the
format1.html template and pass those results to Apache::SSI
The result is a nice and tidy way to control any custom headers, 
footers, background colors, or images in a single html file.

=head1 NOTES

As of 0.02, TEMPLATE is no longer relative to the ServerRoot.



( run in 1.046 second using v1.01-cache-2.11-cpan-e1769b4cff6 )