Apache2-Controller

 view release on metacpan or  search on metacpan

lib/Apache2/Controller/Render/Template.pm  view on Meta::CPAN

 }

 __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 %]


=head1 DESCRIPTION

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

=head1 TEMPLATE OPTIONS

You can specify options for L<Template> in one of two ways:

=head2 DIRECTIVES

By using 
L<Apache2::Controller::Directives/A2C_Render_Template_Opts>:

 <Location '/foo'>
     A2C_Render_Template_Opts INTERPOLATE 1
     A2C_Render_Template_Opts PRE_PROCESS header
     A2C_Render_Template_Opts POST_CHOMP  1
 </Location>

=head2 METHOD template_options()

Or by implementing C<<template_options>> in your controller:

 package MyApp::Controller;
 use base qw( Apache2::Controller Apache2::Controller::Render::Template );
 sub allowed_methods {qw( default )}

 sub template_options {
     my ($self) = @_;
     return {
         INTERPOLATE => 1,
         PRE_PROCESS => 'header',
         POST_CHOMP  =. 1,
     };
 }

 sub default { 
    # ...
 }

=head1 STASH FUNCTIONS

We don't assign any stash functions by default.
If you want to assign consistent stash functions in your controller,
overload C<< render() >>, assign them, and then call C<< SUPER::render() >>.

 package MyApp::ControllerBase;
 use base qw( Apache2::Controller Apache2::Controller::Render::Template );
 use HTML::Entities;

 sub render {
     my ($self) = @_;
     $self->{stash}{encode_entities} = \&encode_entities;
     $self->SUPER::render();
 }

 package MyApp::Controller::Somewhere;
 use base qw( MyApp::ControllerBase );
 # ...
 sub someuri {
     my ($self, @path_args) = @_;
     $self->render();
     return Apache2::Const::HTTP_OK;
 }


=cut

use strict;
use warnings FATAL => 'all';
use English '-no_match_vars';

use Apache2::Const -compile => qw( SERVER_ERROR );
use Apache2::Controller::X;

use File::Spec;
use Template;
use YAML::Syck;
use HTTP::Status qw( status_message );
use Log::Log4perl qw( :easy );

sub _assign_tt_stash_data {
    my ($self) = @_;
    my $stash = $self->{stash} ||= { };
    my $stash_a2c = $stash->{a2c} ||= { };
    my @keys = qw( path_args method controller );
    @{$stash_a2c}{@keys} = @{$self}{@keys};
    return;
}

=head1 METHODS

=head2 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().

=cut

sub render {
    my ($self) = @_;

    DEBUG("beginning render()");

    my $tt = $self->get_tt_obj();
    my $template = $self->detect_template();
    DEBUG("processing template = '$template'");

  # DEBUG(sub { Dump($self->{stash}) });



( run in 0.803 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )