File Coverage

blib/lib/Catalyst/View/Template.pm
Criterion Covered Total %
statement 39 44 88.6
branch 7 14 50.0
condition 2 3 66.6
subroutine 10 11 90.9
pod 6 6 100.0
total 64 78 82.0


line stmt bran cond sub pod time code
1 1     1   2142198 use strict; use warnings;
  1     1   2  
  1         32  
  1         5  
  1         2  
  1         50  
2              
3             package Catalyst::View::Template;
4              
5             our $VERSION = '1.101';
6              
7 1     1   6 use MRO::Compat ();
  1         3  
  1         14  
8 1     1   5 use Catalyst::Utils ();
  1         2  
  1         26  
9              
10 1     1   19 use Catalyst::Component ();
  1         3  
  1         671  
11             our @ISA = 'Catalyst::Component';
12              
13             __PACKAGE__->mk_accessors( my @attribute = qw( class_name template_ext content_type template ) );
14              
15             __PACKAGE__->config(
16             class_name => 'Template',
17             template_ext => '',
18             content_type => 'text/html; charset=utf-8',
19             EVAL_PERL => 0,
20             ENCODING => 'UTF-8',
21             # cannot set INCLUDE_PATH before the app class is set up...
22             );
23              
24             sub new {
25 5     5 1 56387 my ( $class, $c, $args ) = ( shift, @_ );
26 5         20 my $self = $class->next::method( @_ );
27 5         10538 my %config = %$self;
28 5         24 delete @config{ 'catalyst_component_name', @attribute };
29 5 50       41 $config{'INCLUDE_PATH'} = [ $c->path_to( 'root' ) ] unless exists $config{'INCLUDE_PATH'};
30 5         1169 $self->template( $self->new_template( $c, \%config ) );
31 5         29773 $self;
32             }
33              
34             sub new_template {
35 5     5 1 146 my ( $self, $c, $config ) = ( shift, @_ );
36 5         59 Catalyst::Utils::ensure_class_loaded $self->class_name;
37 5 50       935 $self->class_name->new( $config )
38             or die $self->class_name->error;
39             }
40              
41             sub process {
42 13     13 1 304316 my ( $self, $c ) = ( shift, @_ );
43              
44 13         35 my %vars = %{ $c->stash };
  13         44  
45 13   66     990 my $template = $c->stash->{'template'} || $c->action->reverse;
46              
47 13         1432 my $output;
48 13 50       73 $self->render ( $c, $template, \%vars, \$output )
49             ? $self->process_output( $c, $template, \%vars, \$output )
50             : $self->process_error ( $c, $template, \%vars, )
51             }
52              
53             sub render {
54 17     17 1 117403 my ( $self, $c, $template, $vars, $output_ref ) = ( shift, @_ );
55 17 100       155 $template .= $self->template_ext unless 'SCALAR' eq ref $template;
56 17 0       2601 $c->log->debug( sprintf 'Rendering template "%s"', ref $template ? "\\\Q$$template\E" : $template )
    50          
57             if $c->debug;
58 17         143 $self->template->process( $template, $vars, $output_ref );
59             }
60              
61             sub process_output {
62 13     13 1 24154 my ( $self, $c, $template, $vars, $output_ref ) = ( shift, @_ );
63 13 50       69 $c->res->content_type( $self->content_type ) unless $c->res->content_type;
64 13         8797 $c->res->body( $$output_ref );
65 13         854 1;
66             }
67              
68             sub process_error {
69 0     0 1   my ( $self, $c, $template, $vars ) = ( shift, @_ );
70 0           my $error = qq[Couldn't render template "$template": ] . $self->template->error;
71 0           $c->log->error( $error );
72 0           $c->error( $error );
73 0           !1;
74             }
75              
76             1;