File Coverage

blib/lib/Template/Direct.pm
Criterion Covered Total %
statement 44 56 78.5
branch 9 22 40.9
condition n/a
subroutine 10 10 100.0
pod 4 4 100.0
total 67 92 72.8


line stmt bran cond sub pod time code
1             package Template::Direct;
2              
3 2     2   57746 use strict;
  2         6  
  2         182  
4              
5             =head1 NAME
6              
7             Template::Direct - Creates a document page based on template/datasets
8              
9             =head1 SYNOPSIS
10              
11             use Template::Direct;
12              
13             my $template = Template::Direct->new( Location => $fileName/$refName );
14              
15             my $result = $page->compile( Data => {DataSets}, Language => 'en' );
16              
17             =head1 DESCRIPTION
18            
19             Creates, Saves and Manages templates, their languages, the publication of
20             and also does some work with the template design when saving unpublished templates
21              
22             =head1 METHODS
23              
24             =cut
25              
26 2     2   15 use Carp;
  2         5  
  2         183  
27 2     2   1250 use Template::Direct::Page;
  2         6  
  2         56  
28 2     2   5618 use Template::Direct::Data;
  2         7  
  2         61  
29 2     2   1374 use Template::Direct::Directory;
  2         8  
  2         1185  
30              
31             our $VERSION = '1.16';
32             our $config;
33              
34             =head2 I<$class>->new( %properties )
35              
36             Create a new template, takes the arguments:
37              
38             * Directory - Location of all files, base path.
39             * Location - Location of the template (Filename)
40              
41             =cut
42             sub new
43             {
44 2     2 1 25 my ($class, %p) = @_;
45 2 50       18 if(not $p{'Directory'}) { carp "Directory is required for Document Template"; return; }
  0         0  
  0         0  
46 2 50       9 if(not $p{'Location'}) { carp "Location is required for Document Template"; return; }
  0         0  
  0         0  
47            
48 2         6 my $self = bless \%p, $class;
49              
50 2 50       11 if(ref($p{'Directory'}) eq 'Directory') {
51 0         0 $self->{'Dir'} = $p{'Directory'};
52             } else {
53 2         29 $self->{'Dir'} = Template::Direct::Directory->new( $p{'Directory'} );
54             }
55              
56 2 50       9 if(not $self->{'Dir'}) {
57 0 0       0 if(not $p{'Directory'}) {
58 0         0 carp 'Template Error: Directory Required';
59             } else {
60 0         0 carp "Template Error: Directory does not exist '$p{'Directory'}'";
61             }
62 0         0 return;
63             }
64              
65 2         7 return $self;
66             }
67              
68             =head2 I<$template>->loadPage( Language => [] )
69              
70             Load a Template Page object with specific language fallbacks.
71             Returns Template::Direct::Page object.
72              
73             =cut
74             sub loadPage
75             {
76 1     1 1 2 my ($self, %p) = @_;
77 1         6 my $template = $self->load( Language => $p{'Language'} );
78 1 50       4 return if not $template;
79 1         18 my $page = Template::Direct::Page->new( $template,
80             Language => $p{'Language'},
81             Directory => $self->{'Dir'} );
82 1         3 return $page;
83             }
84              
85             =head2 I<$template>->compile( $data, Language => [] )
86              
87             Short cut for loading the page with Languages and then
88             Compiling that page with data. Returns final page string.
89              
90             =cut
91             sub compile {
92 1     1 1 127 my ($self, $data, %p) = @_;
93 1         6 my $page = $self->loadPage(%p);
94 1 50       3 return if not $page;
95 1         6 return $page->compile($data);
96             }
97              
98             =head2 I<$template>->load( %properties )
99              
100             Load a specific version of a template file, returns
101             the template as a string.
102              
103             =cut
104             sub load
105             {
106 2     2 1 8 my ($self, %p) = @_;
107              
108 2         7 my $language = _suitableLanguage( $p{'Language'} );
109 2 50       7 my $filename = $self->{'Location'}.(defined($language) ? '.'.$language : '');
110 2 50       18 my $result = $self->{'Dir'}->loadFile( $filename, Cache => $self->{'Cache'} ? $self->{'Cache'} : 1 );
111 2         25 return $result;
112             }
113              
114             =head2 I<$template>->_suitableLanguage( Template => 'filename', Language => [] )
115              
116             Returns a suitable language to use for this template, given what exists.
117              
118             =cut
119             sub _suitableLanguage
120             {
121 2     2   5 my ($self, %p) = @_;
122 2         4 my $dir = $self->{'Dir'};
123 2         4 my $language = $p{'Language'};
124              
125 2 50       15 if(UNIVERSAL::isa($language, '')) {
126 0         0 $language = [$language];
127             }
128              
129 2         3 foreach my $lang (@{$language}) {
  2         6  
130 0 0       0 if($dir->loadFile($p{'Template'}.".".$lang)) {
131 0         0 return $lang;
132             }
133             }
134              
135 2         6 return;
136             }
137              
138             =head1 AUTHOR
139              
140             Copyright, Martin Owens 2008, AGPL
141              
142             =cut
143             1;