The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Apache2::Controller::Render::Template - A2C render() with Template Toolkit

SYNOPSIS

 # apache2 config file

 PerlModule Apache2::Controller::Directives

 # location of templates - must be defined
 A2CRenderTemplateDir           /var/myapp/templates

 # looks for templates in /var/myapp/templates/foo/
 <Location /foo>
    SetHandler modperl
    PerlInitHandler MyApp::Dispatch::Foo
 </Location>

 # see L<Apache2::Controller::Dispatch> for A2C Dispatch inheritance.

 package MyApp::C::Foo;  # let's assume this controller was dispatched

 use strict;
 use warnings;

 use base qw(
    Apache2::Controller
    Apache2::Controller::Render::Template
    MyApp::Model::Methods
    MyApp::Security
 );

 use Apache2::Const -complie => qw( OK );

 my @ALLOWED_METHODS = qw( default );

 # suppose MyApp::Model::Methods connects dbh in startup sequence
 # and suppose MyApp::Security implements my_detaint_path_args()
 # to call a branch function for 'name' that detaints [ last, first ]

 sub default {
    my ($self) = @_;
    my @path_args = $self->my_detaint_path_args('name'); # from $self->{path_args}

    $self->{stash}{creditcards} = $self->{dbh}->fetchall_arrayref(
        q{  SELECT ccnum, exp, addr1, zip, cac 
            FROM customer_credit_cards 
            WHERE lname = ? AND fname = ?
        }, undef, @path_args
    );

    # request was like http://myserver.xyz/foo/Larry/Wall

    $self->render();    # renders /var/myapp/templates/foo/default.html
    return Apache2::Const::OK;

 }

 __END__
 [%# /var/myapp/templates/foo/default.html %]
 <p>Here is the credit card info you requested for 
 everyone named [% path_args.reverse.join(' ') %]:</p>
 <ul>
 [% FOREACH card = creditcards %]
    [% FOREACH field = ['ccnum','exp','addr1','zip','cac'] %]
    <li><strong>[% field %]:</strong> [% card.$field %]</li>
    [% END %]
 [% END %]
 </ul>
 [%# end template toolkit file %]

DESCRIPTION

This module provides a nice rendering mechanism for Apache2::Controller.

STASH FUNCTIONS

Several subroutine references are automatically included in the stash for ease of use.

escape_html

Escape '<' and '>' characters using HTML::Entities.

uri_escape

See URI::Escape.

Dump

See YAML::Syck.

METHODS

render

render() accumulates template output into a variable before printing, so it may use a lot of memory if you expect a large data set.

It does this so it can intercept Template errors and kick up an exception to be printed using your error templates. See error().

render_fast

So if you are planning to get a large data set, you probably want to use $self->render_fast() and put the database query handle somewhere in $self->{stash} and call fetchrow() in a Template block.

With render_fast(), Template->process() outputs directly to Apache2::Request->print(). So if a Template error is encountered, some output may have already been sent to the browser, resulting in a completely screwed up screen when the exception is kicked back up to the server.

Tip: if you plan to use render_fast(), write a test suite that tests the output of your page.

Of course you could bypass rendering altogether and just use $self->print(). (Remember that $self is subclassed Apache2::Request.) Or maybe you should implement an ajax style control in the template and put a limit frame on the query above, or use a paging lib, etc. ...

error

If your template directory contains a subdirectory named 'error', then when the controller throws an exception, the exception object will be passed to a selected error template as 'X' in the stash. It also sets status (number) and status_line (from HTTP::Status::status_message() or from the values set in the Apache2::Controller::X exception).

If you have a template $template_dir/error/$status.html, where $status is the numeric http status code, then it will use that template.

For example:

 203 HTTP_NON_AUTHORITATIVE     => error/203.html
 400 HTTP_BAD_REQUEST           => error/400.html
 404 NOT_FOUND                  => error/404.html
 500 HTTP_INTERNAL_SERVER_ERROR => error/500.html

For example, $template_dir/error/400.html or $template_dir/error/403.html.

Otherwise it will look for $template_dir/error/default.html and try to use that, otherwise it will give up.

error() remembers across requests whether you do or don't have error templates for certain messages in the appropriate template directory, so it will be faster the second time around if you use error/default.html.

For a reference list of status and messages, see Apache2::Controller.

Since render_fast() is incompatible if a template rendering error occurs, render_fast() turns off the use of error() and relies on standard Apache2 error messages (or the custom message set in the exception object) and relies on the browser to display them.

detect_template

This is called internally by the render methods, but you can use it to figure out the default template from where you are.

To override the auto-select template, just set $self->{template} before you call <render()>.

It looks for templates in a computed directory. The directory where it looks will always be the directory set with the A2CRenderTemplateDir directive in the config file, appended with the current request location, i.e. the directory of the Location directive in the config file, appended with relative_uri, appended with method name and '.html'.

 A2CRenderTemplateDir + location + relative_uri + method.html

For example, the sequence in SYNOPSIS above renders the file /var/myapp/templates/foo/default.html .

Suppose the dispatch class above dispatches sub-path uris starting with 'bar/biz' to another controller. That controller would look for templates in the directory /var/myapp/templates/foo/bar/biz/methodname.html.

Example:

 Request: http://myserver.xyz/foo/bar/biz/baz/boz/noz

 location = /foo

 relative_uri = bar/biz

 controller MyApp::C::Foo::Bar::Biz  # mapped in your A2C Dispatch

 found method = baz

 path_args = [ boz, noz ]

 template = /var/myapp/templates + /foo + /bar/biz + /baz.html

 /var/myapp/templates/foo/bar/biz/baz.html

$self->{relative_uri} is the uri relative to the location, so in other words:

  location + relative_uri == full uri - path args

See Apache2::Controller::Dispatch::Simple.

detect_template_include_path

Like <detect_template()>, this detects the appropriate include path for the template toolkit object, sets it as <$self-{template_include_path}>> and returns it.

This is the directive A2CRenderTemplateDir plus the <Location>. So if you set up your handler to process everything under location /render, and A2CRenderTemplateDir was /var/www/mytemplates, the include_path used for the TT object would be /var/www/mytemplates/render/.

get_tt_obj

Get the Template object set up with the appropriate include directory set from the directive <A2CRenderTemplateDir>.

Directive A2CRenderTemplateOpts sets default new() options for Template.

SEE ALSO

Apache2::Controller

Apache2::Controller::Render::Template

Apache2::Controller::X

AUTHOR

Mark Hedges, <hedges at--! scriptdolphin.org>

COPYRIGHT & LICENSE

Copyright 2008 Mark Hedges, all rights reserved.

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