| Catalyst-View-HTML-Template-Compiled documentation | Contained in the Catalyst-View-HTML-Template-Compiled distribution. |
Catalyst::View::HTML::Template::Compiled - HTML::Template::Compiled View Class
# use the helper
script/myapp_create.pl view HTML::Template::Compiled HTML::Template::Compiled
# lib/MyApp/View/HTML/Template.pm
package MyApp::View::HTML::Template::Compiled;
use base 'Catalyst::View::HTML::Template::Compiled';
__PACKAGE__->config(
use_default_path => 0, # defaults to 1
# any HTML::Template::Compiled configurations items go here
# see HTML::Template::Compiled documentation for more details
);
1;
# Meanwhile, maybe in an 'end' action
$c->forward('MyApp::View::HTML::Template::Compiled');
This is the HTML::Template::Compiled view class. Your subclass should inherit from this
class.
Internally used by Catalyst. Used to configure some internal stuff.
Renders the template specified in $c-stash->{template} >, $c-request->match >, $c-config->{template}->{filename} > or __PACKAGE__-config->{filename} >.
Template params are set up from the contents of $c-stash >,
augmented with base set to $c-req->base >, name to
$c-config->{name} > and c to $c . Output is stored in $c-response->body >.
Pretty much the first thing called by process . Only used for sub-classing. Return a i<true>-value if everything is okay, otherwise process will fail.
Will be called right before process finishes. Only used for sub-classing. Whatever it returns, process will return.
Creates the HTML::Template::Compiled object.
On success, returns the filename to be rendered; undef otherwise.
Accessor to the HTML::Template::Compiled object.
May returns undef then the object has not yet been created
or creating has failed.
First thing before render is called.
Assigns the parameters like the ones from the
stash.
This is where the rendering magic happens. Returns the rendered output on success, or undef otherwise.
Tries to find the right template to render. Returns its filename or undef. Actually only used internally.
Returns a array ref with paths used to find the templates in.
use_default_path: if set, will include $c->config->{root} and
$c->config->{root} . '/base' to look for the template. Defaults to 1.
This also allows your view subclass to pass additional settings to the
HTML::Template::Compiled config hash.
A list of names that are used to locate configuration parameters
for the view inside $c-config >.
Normally all methods are called with the $c as the first parameter.
Just to insure that you have it as a method it case you need it. :)
Will be initializes by new .
Sascha Kiefer, esskar@cpan.org
This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself.
| Catalyst-View-HTML-Template-Compiled documentation | Contained in the Catalyst-View-HTML-Template-Compiled distribution. |
package Catalyst::View::HTML::Template::Compiled; use strict; use base 'Catalyst::Base'; use HTML::Template::Compiled (); use Path::Class (); our $VERSION = '0.16'; __PACKAGE__->mk_accessors(qw/htc catalyst/);
sub new { my ( $class, $c, $arguments ) = @_; my $config = { use_perl => 0, %{ $class->config }, %{ $arguments }, }; my @config_names = $class->config_names; foreach (@config_names) { if(exists $c->config->{$_}) { $config = { %{ $config }, %{ $c->config->{$_} || {} }, }; } } $config->{use_default_path} = 1 unless defined $config->{use_default_path}; my $self = $class->NEXT::new( $c, { %$config }, ); $self->catalyst( $c ); $self->config($config); return $self; }
sub process { my ( $self, $c ) = @_; return 0 unless $self->prepare_process( $c ); return 0 unless $self->prepare_htc( $c ); return 0 unless $self->prepare_render( $c ); my $retval = 0; if( $retval = defined( my $body = $self->render( $c ) ) ) { $c->res->headers->content_type('text/html; charset=utf-8') unless ( $c->response->headers->content_type ); $c->response->body($body); } return $self->finalize_process( $c ); }
sub prepare_process { my ($self, $c ) = @_; return 1; }
sub finalize_process { my ($self, $c ) = @_; return 1; }
sub prepare_htc { my ($self, $c ) = @_; $c ||= $self->catalyst; my $filename = $self->template( $c ); unless( $filename && $self->htc ) { my $error = "Nothing to render."; $c->log->error($error); $c->error($error); return undef; } return $self->htc->get_file; }
sub prepare_render { my ($self, $c ) = @_; $c ||= $self->catalyst; $self->htc->param( base => $c->request->base, name => $c->config->{name}, c => $c, %{ $c->stash } ); return 1; }
sub render { my ($self, $c ) = @_; $c ||= $self->catalyst; $c->log->debug(sprintf('Trying to render template "%s" ...', $self->htc->get_file)) if $c->debug; my $body; eval { $body = $self->htc->output }; if ( my $error = $@ ) { chomp $error; $error = sprintf( qq/Couldn't render template "%s". Error: "%s"/, $self->htc->get_file, $error ); $c->log->error($error); $c->error($error); return undef; } return $body; }
sub template { my ($self, $c ) = @_; $c ||= $self->catalyst; $c->log->debug('Finding template to render ...') if $c->debug; my %options = ( %{ $self->config }, path => $self->path( $c ), ); my $extension = $self->config->{extension} || ''; if ($extension) { $extension = ".$extension" unless substr( $extension, 0, 1 ) eq '.'; } my $prefix = $self->config->{prefix} || ''; my @filenames = ( $c->stash->{template}, $prefix . $c->request->match . $extension, $prefix . $c->request->action . $extension, $self->config->{filename}, $c->config->{template}->{filename}, ); my $htc; foreach my $filename (@filenames) { next unless $filename; $options{filename} = $filename; eval { $htc = HTML::Template::Compiled->new(%options); }; last unless $@; $c->log->debug( "HTC error: $@" ) if $c->debug; } $self->htc( $htc ); return $options{filename}; }
sub path { my ($self, $c) = @_; $c ||= $self->catalyst; my $templ_path = $self->config->{path} || ''; $templ_path = [$templ_path] unless 'ARRAY' eq ref $templ_path; my $path = $self->_build_path( $templ_path, ( map { $c->path_to($_) } @$templ_path ), ( $self->config->{use_default_path} ? ( $c->config->{root}, $c->config->{root} . '/base' ) : () ), ); return $path; } sub _build_path { my ( $self, @paths ) = @_; my @retval = (); foreach my $path (@paths) { next unless defined $path; if ( ref($path) eq 'ARRAY' ) { push @retval, $self->_build_path( @{$path} ); } elsif ( ref($path) eq 'Path::Class::Dir' ) { # stringify it push @retval, "" . $path->absolute; } else { push @retval, $self->_build_path( Path::Class::dir($path) ); } } return wantarray ? @retval : [@retval]; }
sub config_names { return qw/View::HTML::Template::Compiled V::HTML::Template::Compiled View::HTC V::HTC template/; }
1;