File Coverage

lib/Web/Components/Role/TT.pm
Criterion Covered Total %
statement 42 42 100.0
branch 10 12 100.0
condition 4 8 50.0
subroutine 11 11 100.0
pod 1 1 100.0
total 68 74 94.5


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