Apache-Template

 view release on metacpan or  search on metacpan

lib/Template/Service/Apache.pm  view on Meta::CPAN

        # save error as exception for params() to add to template vars
        $self->{ TEMPLATE_ERROR } = Template::Exception->new(
            Template::Constants::ERROR_FILE, $template
        );
        
        # if there is an ERROR template defined then we attempt to 
        # fetch it as a substitute for the original template.  Note 
        # that we must fetch it from the regular template providers
        # in the Template::Context because they honour the INCLUDE_PATH 
        # parameters whereas the ROOT_PROVIDER expects an absolute file
        
        if ($template = $self->{ ERROR }) {
            eval { $template = $self->{ CONTEXT }->template($template) };
            if ($@) {
                $r->log_reason($self->{ TEMPLATE_ERROR } . " / $@", $filename);
                return SERVER_ERROR;
            }
        }
        else {
            $r->log_reason($template, $filename);
            return SERVER_ERROR;
        }
    }
    
    return $template;
}


#------------------------------------------------------------------------
# params($request, $params)
#
# Create a set of processing parameters (i.e. template variables) for
# the request.
#------------------------------------------------------------------------

sub params {
    my ($self, $request, $params) = @_;
    $params ||= { };

    my $plist = $self->{ SERVICE_PARAMS };
    my $all = $plist->{ all };

    return $params unless keys %$plist;
    $request = Apache::Request->new($request);

    $params->{ env } = { %{ $request->subprocess_env() } }
        if $all or $plist->{ env };

    $params->{ uri } = $request->subprocess_env('REDIRECT_URL') || $request->uri()
        if $all or $plist->{ uri };

    $params->{ pnotes } = $request->pnotes()
        if $all or $plist->{ pnotes };

    $params->{ params } = { %{ $request->parms() } }
        if $all or $plist->{ params };

    $params->{ request } = $request
        if $all or $plist->{ request };

    if ($all or $plist->{ uploads }) {
        my @uploads = $request->upload;
        $params->{ uploads } = \@uploads;
    }

    $params->{ cookies } = { 
        map { $1 => escape_uri($2) if (/([^=]+)=(.*)/) }
        grep(!/^$/, split(/;\s*/, $request->header_in('cookie'))),
    } if $all or $plist->{ cookies };
    
    # add any error raised by main template failure
    $params->{ error } = $self->{ TEMPLATE_ERROR };

    return $params;
}


#------------------------------------------------------------------------
# headers($request, $template, $content_ref)
#
# Set and then send the required http headers.
#------------------------------------------------------------------------

sub headers {
    my ($self, $r, $template, $content) = @_;
    my $headers = $self->{ SERVICE_HEADERS };
    my $all = $headers->{ all };

    $r->content_type($self->{ CONTENT_TYPE })
        if $all or $headers->{ type };
    $r->headers_out->add('Content-Length' => length $$content)
        if $all or $headers->{ length };
    $r->headers_out->add('Last-Modified'  => ht_time($template->modtime()))
        if $all or $headers->{ modified } and $template;
    $r->headers_out->add('E-tag' => sprintf q{"%s"}, md5_hex($$content))
        if $all or $headers->{ etag };
    $r->send_http_header;
}


#------------------------------------------------------------------------
# _init()
#
# In additional to the regular template providers (Template::Provider
# objects) created as part of the context initialisation and used to
# deliver templates loaded via INCLUDE, PROCESS, etc., we also create
# a single additional provider responsible for loading the main
# template.  We do this so that we can enable its ABSOLUTE flag,
# allowing us to specify a requested template by absolute filename (as
# Apache provides for us in $r->filename()) but without forcing all
# other providers to honour the ABSOLUTE flag.  We pre-create a PARSER
# object (Template::Parser) which can be shared across all providers.
#------------------------------------------------------------------------

sub _init {
    my ($self, $config) = @_;

    # create a parser to be shared by all providers
    $config->{ PARSER } ||= Template::Config->parser($config) 
        || return $self->error(Template::Config->error());

    # create a provider for the root document
    my $rootcfg = {



( run in 2.200 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )