Apache-Template

 view release on metacpan or  search on metacpan

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


    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 = {
        ABSOLUTE => 1,
        map { exists $config->{ $_ } ? ($_, $config->{ $_ }) : () }
        qw( COMPILE_DIR COMPILE_EXT CACHE_SIZE PARSER ),
    };

    my $rootprov = Template::Config->provider($rootcfg)
        || return $self->error(Template::Config->error());

    # now let the Template::Service superclass initialiser continue
    $self->SUPER::_init($config)
        || return undef;

    # save reference to root document provider
    $self->{ ROOT_PROVIDER } = $rootprov;

    # determine content type or use default
    $self->{ CONTENT_TYPE } = $config->{ CONTENT_TYPE } || $CONTENT_TYPE;


    # if TT2Headers not explicitly defined then we default it to 
    # just send the Content-Type, for the simple cases and backwards
    # compatibility with earlier versions (0.08 and earlier) where
    # the Content-Type was always sent regardless

    $config->{ SERVICE_HEADERS } = ['type']
        unless $config->{ SERVICE_HEADERS };

    # extract other relevant SERVICE_* config items
    foreach (qw( SERVICE_HEADERS SERVICE_PARAMS )) {
        my $item = $config->{ $_ } || [ ];
        $self->{ $_ } = { map { $_ => 1 } @$item };
    }
    
    
    return $self;
}
    
1;



( run in 1.017 second using v1.01-cache-2.11-cpan-13bb782fe5a )