1package Text::MicroMason::ApacheHandler; 2 3use Apache::Constants; 4use Apache::Request; 5 6use Text::MicroMason::Base; 7 8###################################################################### 9 10my %configs; 11 12sub handler ($$) { 13 my ($package, $r) = @_; 14 15 my $apache = Apache::Request->instance( $r ); 16 17 my $file = $apache->filename; 18 19 # $apache->document_root; 20 my $syntax = $apache->dir_config('MicroMasonSyntax') || 'HTMLMason'; 21 my @mixins = $apache->dir_config->get('MicroMasonMixins'); 22 my @attrs = $apache->dir_config->get('MicroMasonAttribs'); 23 24 my %seen; 25 unshift @attrs, ( map "-$_", grep { ! $seen{$_} ++ } ( @mixins, $syntax ) ); 26 27 my $config = join ' ', @attrs; 28 29 my $mason = ( $configs{$config} ||= Text::MicroMason::Base->new( @attrs ) ); 30 31 my $template = $mason->compile( file => $file ); 32 33 $apache->content_type( 'text/html' ); 34 # $apache->header_out(); 35 36 local $Text::MicroMason::Commands::r = $apache; 37 print $template->( $apache->param() ); 38 39 return Apache::Constants::OK(); 40} 41 42sub configure { 43 my $apache = Apache::Request->instance( shift ); 44 45 my $file = $apache->filename; 46 47 # $apache->document_root; 48 my $syntax = $apache->dir_config('MicroMasonSyntax') || 'HTMLMason'; 49 my @mixins = $apache->dir_config->get('MicroMasonMixins'); 50 my @attrs = $apache->dir_config->get('MicroMasonAttribs'); 51 52 my %seen; 53 unshift @attrs, ( map "-$_", grep { ! $seen{$_} ++ } ( @mixins, $syntax ) ); 54 55 my $config = join ' ', @attrs; 56 57 my $mason = ( $configs{$config} ||= Text::MicroMason::Base->new( @attrs ) ); 58} 59 60###################################################################### 61 62sub translate_params { 63 MasonAllowGlobals => [ -AllowGlobals, allow_globals => \$1 ], 64 MasonCompRoot => [ -TemplateDir, template_root => \$1 ], 65} 66 67###################################################################### 68 691; 70 71__END__ 72 73###################################################################### 74 75=head1 NAME 76 77Text::MicroMason::ApacheHandler - Use MicroMason from mod_perl 78 79 80=head1 SYNOPSIS 81 82In your httpd.conf or equivalent Apache configuration file: 83 84 PerlModule Text::MicroMason::ApacheHandler 85 86 <Files *.mm> 87 SetHandler perl-script 88 PerlHandler Text::MicroMason::ApacheHandler 89 </Files> 90 91In your document root or other web-accessible directory: 92 93 <% my $visitor = $r->connection->remote_host(); %> 94 <html> 95 Hello there <%= $visitor %>! 96 The time is now <%= localtime() %>. 97 </html> 98 99=head1 DESCRIPTION 100 101B<Caution:> This module is new, experimental, and incomplete. Not intended for production use. Interface subject to change. If you're interested in this capability, your feedback would be appreciated. 102 103=head2 Configuration 104 105The following configuration parameters are supported: 106 107=over 4 108 109=item MicroMasonSyntax 110 111 PerlSetVar MicroMasonSyntax HTMLMason 112 113Name of the syntax class that will compile the templates. Defaults to HTMLMason. 114 115=item MicroMasonMixins 116 117 PerlAddVar MicroMasonMixins Safe 118 PerlAddVar MicroMasonMixins CatchErrors 119 120List of additional mixin classes to be enabled. 121 122=item MicroMasonAttribs 123 124 PerlAddVar MicroMasonAttribs "-AllowGlobals, allow_globals => '$r'" 125 126Allows for any set of attributes to be defined. Mixin names prefaced with a dash can also be included. 127 128=back 129 130=head1 SEE ALSO 131 132For an overview of this templating framework, see L<Text::MicroMason>. 133 134This is a mixin class intended for use with L<Text::MicroMason::Base>. 135 136For distribution, installation, support, copyright and license 137information, see L<Text::MicroMason::Docs::ReadMe>. 138 139=cut 140