Apache::JemplateFilter - Jemplate complie filter for Apache


Apache-JemplateFilter documentation Contained in the Apache-JemplateFilter distribution.

Index


Code Index:

NAME

Top

Apache::JemplateFilter - Jemplate complie filter for Apache

VERSION

Top

Version 0.02

SYNOPSIS

Top

in httpd.conf

    PerlModule Apache::Filter

    <Location /foo/tmpl/>
        SetHandler      perl-script
        PerlSetVar      Filter On
        PerlHandler     Apache::JemplateFilter
    </Location>

Requests for /foo/tmpl/* are compiled by Jemplate.

DESCRIPTION

Top

This module is Jemplate compile filter for Apache1.x (mod_perl1.x).

For Apache2, use Apache2::JemplateFilter.

FUNCTIONS

Top

handler

AUTHOR

Top

FUJIWARA Shunichiro, <fujiwara at topicmaker.com>

BUGS

Top

Please report any bugs or feature requests to bug-apache-jemplatefilter at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Apache-JemplateFilter. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SEE ALSO

Top

Apache::Filter Jemplate Apache2::JemplateFilter

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Apache::JemplateFilter

COPYRIGHT & LICENSE

Top


Apache-JemplateFilter documentation Contained in the Apache-JemplateFilter distribution.
package Apache::JemplateFilter;

use warnings;
use strict;
use Apache::Filter;
use Apache::Constants qw( OK HTTP_OK );
use Apache::Log;
use Apache::File;
use Jemplate 0.12;

our $VERSION = '0.02';
our $cache   = {};

sub handler {
    my $r   = shift;
    my $log = $r->server->log;

    $r = $r->filter_register;

    my ( $fh, $status ) = $r->filter_input();
    return $status unless $status == OK;

    my $filename = $r->filename;

    # cache hit ?
    my $c = $cache->{$filename};
    if ( $c && $c->{mtime} == $r->mtime ) {
        $r->set_content_length( length $c->{js} );
        $r->content_type('application/x-javascript');
        $r->send_http_header($r->content_type);
        $r->print( $c->{js} );
        return OK;
    }

    my $buf;
    {
        local $/ = undef;
        $buf = <$fh>;
    }

    ( my $tmpl_filename = $filename ) =~ s/.*[\/\\]//;
    my $jemplate = Jemplate->new( EVAL_JAVASCRIPT => 1 );
    my $js;
    eval {
        $js =
            $jemplate->_preamble
            . $jemplate->compile_template_content( $buf, $tmpl_filename );
    };
    if ($@) {
        my $msg = sprintf "%s compile error while processing %s. %s",
            __PACKAGE__,
            $r->uri,
            $@;
        $msg =~ s/\'/\\'/g;         # '
        $msg =~ s/[\x0A\x0D]/ /g;
        $js  = "throw('$msg')";
        $log->error($msg);
    }
    $r->set_content_length( length $js );
    $r->content_type('application/x-javascript');
    $r->send_http_header($r->content_type);
    $r->print($js);
    $cache->{$filename} = { js => $js, mtime => $r->mtime };
    return OK;
}

1; # End of Apache::JemplateFilter