| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Web::Components::Role::TT; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 1813 | use 5.010001; | 
|  | 1 |  |  |  |  | 2 |  | 
| 4 | 1 |  |  | 1 |  | 4 | use namespace::autoclean; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 5 | 1 |  |  | 1 |  | 57 | use version; our $VERSION = qv( sprintf '0.7.%d', q$Rev: 1 $ =~ /\d+/gmx ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 94 | use File::DataClass::Constants qw( EXCEPTION_CLASS NUL TRUE ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 62 |  | 
| 8 | 1 |  |  | 1 |  | 417 | use File::DataClass::Types     qw( Directory Object ); | 
|  | 1 |  |  |  |  | 23748 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 9 | 1 |  |  | 1 |  | 1187 | use Template; | 
|  | 1 |  |  |  |  | 13088 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 10 | 1 |  |  | 1 |  | 5 | use Unexpected::Functions      qw( PathNotFound throw ); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 11 | 1 |  |  | 1 |  | 360 | use Moo::Role; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 7 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | requires qw( config ); # layout skin tempdir vardir | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | # Attribute constructors | 
| 16 |  |  |  |  |  |  | my $_build_templates = sub { | 
| 17 | 2 |  |  | 2 |  | 21275 | my $self = shift; my $conf = $self->config; my $dir; | 
|  | 2 |  |  |  |  | 42 |  | 
|  | 2 |  |  |  |  | 3 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 2 | 100 |  |  |  | 22 | $conf->can( 'vardir' ) and $dir = $conf->vardir->catdir( 'templates' ); | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 2 | 100 | 66 |  |  | 583 | return ($dir && $dir->exists) ? $dir : $conf->root->catdir( 'templates' ); | 
| 22 |  |  |  |  |  |  | }; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | my $_build__templater = sub { | 
| 25 | 2 |  |  | 2 |  | 464 | my $self        =  shift; | 
| 26 | 2 |  |  |  |  | 10 | 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 |  |  |  | 1112 | my $template    =  Template->new( $args ) or throw $Template::ERROR; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 2 |  |  |  |  | 17331 | 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 = $page->{layout} // $plate->{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 | 14488 | my ($self, $stash) = @_; $stash //= {}; | 
|  | 5 |  |  |  |  | 23 |  | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 5 |  |  |  |  | 9 | my $result = NUL; | 
| 84 | 5 |  | 33 |  |  | 42 | my $conf   = $stash->{config} //= $self->config; | 
| 85 | 5 |  |  |  |  | 14 | my $layout = $_layout->( $conf, $stash ); | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 5 | 100 |  |  |  | 16 | if (ref $layout) { | 
| 88 | 2 | 100 |  |  |  | 11 | $self->_templater->process( $layout, $stash, \$result ) | 
| 89 |  |  |  |  |  |  | or throw $self->_templater->error; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | else { | 
| 92 | 3 |  |  |  |  | 12 | my $path = $self->$_rel_template_path( $conf, $stash, $layout ); | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | # uncoverable branch true | 
| 95 | 2 | 50 |  |  |  | 1683 | $self->_templater->process( $path, $stash, \$result ) | 
| 96 |  |  |  |  |  |  | or throw $self->_templater->error; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 3 |  |  |  |  | 38722 | return $result; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | 1; | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | __END__ |