File Coverage

lib/Web/Components/Role/TT.pm
Criterion Covered Total %
statement 38 38 100.0
branch 6 8 100.0
condition 2 5 40.0
subroutine 11 11 100.0
pod 1 1 100.0
total 58 63 95.2


line stmt bran cond sub pod time code
1             package Web::Components::Role::TT;
2              
3 1     1   1672 use 5.010001;
  1         3  
4 1     1   4 use namespace::autoclean;
  1         1  
  1         5  
5 1     1   55 use version; our $VERSION = qv( sprintf '0.6.%d', q$Rev: 1 $ =~ /\d+/gmx );
  1         1  
  1         4  
6              
7 1     1   84 use File::DataClass::Constants qw( EXCEPTION_CLASS NUL TRUE );
  1         1  
  1         50  
8 1     1   417 use File::DataClass::Types qw( Directory Object );
  1         23098  
  1         7  
9 1     1   1164 use Template;
  1         13264  
  1         25  
10 1     1   5 use Unexpected::Functions qw( PathNotFound throw );
  1         1  
  1         6  
11 1     1   312 use Moo::Role;
  1         2  
  1         7  
12              
13             requires qw( config ); # layout root skin tempdir
14              
15             # Attribute constructors
16             my $_build__templater = sub {
17 1     1   464 my $self = shift;
18 1         6 my $args = {
19             COMPILE_DIR => $self->config->tempdir->catdir( 'ttc' ),
20             COMPILE_EXT => 'c',
21             ENCODING => 'utf8',
22             RELATIVE => TRUE,
23             INCLUDE_PATH => [ $self->templates->pathname ], };
24             # uncoverable branch true
25 1 50       585 my $template = Template->new( $args ) or throw $Template::ERROR;
26              
27 1         16461 return $template;
28             };
29              
30             # Public attributes
31             has 'templates' => is => 'lazy', isa => Directory, coerce => TRUE,
32 1     1   13292 builder => sub { $_[ 0 ]->config->root->catdir( 'templates' ) };
33              
34             # Private attributes
35             has '_templater' => is => 'lazy', isa => Object, builder => $_build__templater;
36              
37             # Private functions
38             my $_layout = sub {
39             my ($conf, $stash) = @_;
40              
41             my $page = $stash->{page} //= {};
42             my $plate = $stash->{template} //= {};
43             my $layout = $page->{layout} // $plate->{layout} // $conf->layout;
44              
45             return $plate->{layout} = $page->{layout} = $layout;
46             };
47              
48             my $_skin = sub {
49             my ($conf, $stash) = @_;
50              
51             my $plate = $stash->{template} //= {};
52             my $skin = $plate->{skin} // $stash->{skin} // $conf->skin;
53              
54             return $plate->{skin} = $stash->{skin} = $skin;
55             };
56              
57             my $_rel_template_path = sub {
58             my ($self, $conf, $stash, $layout) = @_; my $templates = $self->templates;
59              
60             my $path = $templates->catfile( $_skin->( $conf, $stash ), "${layout}.tt" );
61              
62             $path->exists and return $path->abs2rel( $templates );
63              
64             my $alt = $templates->catfile( $conf->skin, "${layout}.tt" );
65              
66             $alt->exists or throw PathNotFound, [ $path ];
67              
68             return $alt->abs2rel( $templates );
69             };
70              
71             # Public methods
72             sub render_template {
73 4   50 4 1 13445 my ($self, $stash) = @_; $stash //= {};
  4         10  
74              
75 4         4 my $result = NUL;
76 4   33     23 my $conf = $stash->{config} //= $self->config;
77 4         9 my $layout = $_layout->( $conf, $stash );
78              
79 4 100       8 if (ref $layout) {
80 2 100       6 $self->_templater->process( $layout, $stash, \$result )
81             or throw $self->_templater->error;
82             }
83             else {
84 2         4 my $path = $self->$_rel_template_path( $conf, $stash, $layout );
85              
86             # uncoverable branch true
87 1 50       1403 $self->_templater->process( $path, $stash, \$result )
88             or throw $self->_templater->error;
89             }
90              
91 2         34615 return $result;
92             }
93              
94             1;
95              
96             __END__