Apache::Sandwich - Layered document (sandwich) maker


 SetHandler  perl-script
 PerlHandler Apache::Sandwich
 PerlSetVar SandwichHandler default-handler


The Apache::Sandwich module allows you to add a per-directory custom "header" and/or "footer" content to a given uri. Only works with "GET" requests. Output of combined parts is forced to text/html. The handler for the sandwiched document is specified by the SandwichHandler configuration variable. If it is not set, "default-handler" is used.

The basic concept is that the concatenation of the HEADER and FOOTER parts with the sandwiched file inbetween constitute a complete valid HTML document.

Here's a configuration example:

 #in httpd.conf or .htaccess

 <Location /foo>
  #any request for /foo and it's document tree
  #are run through our Apache::Sandwich::handler
  SetHandler  perl-script
  PerlHandler Apache::Sandwich

  #we send this file first
  PerlSetVar HEADER "/my_header.html"

  #requested uri (e.g. /foo/index.html) is sent in the middle

  #we send output of this mod_perl script last  
  PerlSetVar FOOTER "/perl/"

With the above example, one must be careful not to put graphics within the same directory, otherwise they will be Sandwiched also.

Here's another example which only Sandwiches the files we want (using Apache 1.3 syntax). In this example, all *.brc files are sandwiched using the defined HEADER and FOOTER parts, with the file itself assumed to be a plain text file (or static HTML file). All *.sbrc files are similarly sandwiched, except that the file itself is assumed to be an "shtml" file, i.e., it is given to the SSI handler for server side includes to be processed.

 # filter *.brc files through Sandwich maker, but define
 # HEADER/FOOTER per section below
 <FilesMatch "\.brc$">
  SetHandler  perl-script
  PerlHandler Apache::Sandwich
 <FilesMatch "\.sbrc$">
  SetHandler  perl-script
  PerlHandler Apache::Sandwich
  PerlSetVar SandwichHandler server-parsed
 # now specify the header and footer for each major section
 <Location /misc>
   PerlSetVar HEADER "/misc/HEADER.shtml ADS.shtml"
   PerlSetVar FOOTER "/misc/FOOTER.html"
 <Location /getting_started>
   PerlSetVar HEADER "/getting_started/HEADER.shtml ADS.shtml"
   PerlSetVar FOOTER "/misc/FOOTER.html"

Note that in this example, the file ADS.shtml is included from the same directory as the requested file.

The files referenced in the HEADER and FOOTER variables are fetched using the "GET" method and may be of any type file the server can process.

Sandwiching mod_perl Programs

The helper function insert_parts() can be used to make your CGI programs look like the rest of your pages. A simple script would be:

 #! /usr/local/bin/perl
 use strict;

 use CGI;
 use Apache::Sandwich;

 use vars qw($query);

 $query = new CGI or die "Something failed";

 print $query->header();

 print $query->p("Hello, world!");



Headers printed by mod_perl scripts used as a HEADER, FOOTER or in-the-middle uri need to use version 2.37 or higher or headers will show up in the final document (browser window). Suggested work-around (if you don't want to upgrade

 if($ENV{MOD_PERL} and Apache->request->is_main) {
    #send script headers
    print "Content-type: text/html\n\n";
 } else {
    #we're part of a sub-request, don't send headers

Setting $Apache::Sandwich::Debug to 1 will log debugging information into the Apache ErrorLog.


Doug MacEachern. Modifications and cleanup by Vivek Khera.


This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.