File Coverage

blib/lib/WebNano/Renderer/TT.pm
Criterion Covered Total %
statement 58 61 95.0
branch 13 16 81.2
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 83 89 93.2


line stmt bran cond sub pod time code
1             package WebNano::Renderer::TT;
2             BEGIN {
3 1     1   23551 $WebNano::Renderer::TT::VERSION = '0.002';
4             }
5 1     1   8 use strict;
  1         2  
  1         26  
6 1     1   4 use warnings;
  1         2  
  1         27  
7              
8 1     1   3026 use Template;
  1         41522  
  1         36  
9 1     1   987 use Object::Tiny::RW qw/ root _tt _global_path INCLUDE_PATH TEMPLATE_EXTENSION/;
  1         387  
  1         8  
10 1     1   1762 use File::Spec;
  1         3  
  1         1344  
11              
12             sub new {
13 3     3 1 1493 my( $class, %args ) = @_;
14 3         15 my $self = bless {
15             _global_path => [ _to_list( delete $args{INCLUDE_PATH} ) ],
16             root => delete $args{root},
17             TEMPLATE_EXTENSION => delete $args{TEMPLATE_EXTENSION},
18             }, $class;
19             # Use a weakend copy of self so we dont have loops preventing GC from working
20 3         6 my $copy = $self;
21 3         27 Scalar::Util::weaken($copy);
22 3     12   20 $args{INCLUDE_PATH} = [ sub { $copy->INCLUDE_PATH } ];
  12         3301  
23 3         40 $self->_tt( Template->new( \%args ) );
24 3         35903 return $self;
25             }
26              
27              
28             sub _to_list {
29 18 100   18   104 if( ref $_[0] ){
    100          
30 15         20 return @{ $_[0] };
  15         107  
31             }
32             elsif( ! defined $_[0] ){
33 2         15 return ();
34             }
35             else{
36 1         8 return $_[0];
37             }
38             }
39              
40             sub render {
41 11     11 1 4305 my( $self, %params ) = @_;
42 11         16 my $c = $params{c};
43 11         15 my @input_path;
44 11 100       41 if( $c ){
45 7         13 my $path = ref $c;
46 7         41 $path =~ s/.*::Controller(::)?//;
47 7         11 $path =~ s{::}{/};
48 7         11 @input_path = ( $path, @{ $c->template_search_path });
  7         31  
49             }
50 11 100       50 if( !@input_path ){
51 4         10 @input_path = ( '' );
52             }
53 11         16 my @path = @{ $self->_global_path };
  11         255  
54 11         70 for my $sub_path( @input_path ){
55 15         305 for my $root( _to_list( $self->root ) ){
56 29 50       150 if( File::Spec->file_name_is_absolute( $sub_path ) ){
57 0         0 push @path, $sub_path;
58             }
59             else{
60 29         210 push @path, File::Spec->catdir( $root, $sub_path );
61             }
62             }
63             }
64 11         242 $self->INCLUDE_PATH( \@path );
65 11         78 my $template = $params{template};
66 11 100       29 if( !$template ){
67 1         8 my @caller = caller(2);
68 1         3 $template = $caller[3];
69 1         4 $template =~ s/_action$//;
70 1         4 $template =~ s/^.*:://;
71 1 50       40 $template .= '.' . $self->TEMPLATE_EXTENSION if $self->TEMPLATE_EXTENSION;
72             }
73 11         238 my $tt = $self->_tt;
74 11         51 my $output;
75 11 50       38 if( ! $tt->process( $template, \%params, \$output ) ){
76 0         0 warn "Current INCLUDE_PATH: @path\n";
77 0         0 die $tt->error();
78             }
79 11         56715 return $output;
80             }
81              
82             1;
83              
84              
85              
86             =pod
87              
88             =head1 NAME
89              
90             WebNano::Renderer::TT - A Template Toolkit renderer for WebNano with dynamic search paths
91              
92             =head1 VERSION
93              
94             version 0.002
95              
96             =head1 SYNOPSIS
97              
98             use WebNano::Renderer::TT;
99             $renderer = WebNano::Renderer::TT->new( root => [ 't/data/tt1', 't/data/tt2' ] );
100             $out = '';
101             $renderer->render( template => 'template.tt', search_path => [ 'subdir1', 'subdir2' ], output => \$out );
102              
103             =head1 DESCRIPTION
104              
105             This is experimental Template Tookit dynamic renderer for L.
106             Please note that you can use Template Tookit directly in WebNano without this module,
107             what this module adds is way to search for the templates that depends on the
108             controller.
109             When looking for
110             a template file it scans a cartesian product of static set of paths provided
111             at instance creation time and stored in the C attribute and a dynamic
112             set provided to the C method in the C attribute. Additionally it
113             also scans the C in a more traditional and non-dynamic way.
114              
115             =head1 ATTRIBUTES
116              
117             =head2 root
118              
119             =head2 INCLUDE_PATH
120              
121             A mechanism to provide the serach path directly sidestepping the dynamic calculations.
122              
123             Templates that are to be found in C are universal - i.e. can be Cd
124             everywhere.
125              
126             =head2 TEMPLATE_EXTENSION
127              
128             Postfix added to action name to form the template name ( for example 'edit.tt'
129             from action 'edit' and TEMPLATE_EXTENSION 'tt' ).
130              
131             =head1 METHODS
132              
133             =head2 render
134              
135             =head2 new
136              
137             =head1 AUTHOR
138              
139             Zbigniew Lukasiak
140              
141             =head1 COPYRIGHT AND LICENSE
142              
143             This software is Copyright (c) 2010 by Zbigniew Lukasiak .
144              
145             This is free software, licensed under:
146              
147             The Artistic License 2.0
148              
149             =cut
150              
151              
152             __END__