File Coverage

blib/lib/Text/PORE/Template.pm
Criterion Covered Total %
statement 54 64 84.3
branch 7 12 58.3
condition 1 3 33.3
subroutine 11 13 84.6
pod 1 6 16.6
total 74 98 75.5


line stmt bran cond sub pod time code
1             package Text::PORE::Template;
2            
3 1     1   2111 use strict;
  1         2  
  1         80  
4 1     1   7 use Carp;
  1         2  
  1         67  
5 1     1   6 use Exporter;
  1         3  
  1         34  
6 1     1   6 use FileHandle;
  1         3  
  1         9  
7            
8 1     1   1024 use Text::PORE::Parser;
  1         239  
  1         135  
9 1     1   8 use Text::PORE::Globals;
  1         4  
  1         41  
10 1     1   7 use Text::PORE::Volatile;
  1         2  
  1         644  
11            
12             $Text::PORE::Template::VERSION = "0.05";
13            
14             @Text::PORE::Template::ISA = qw(Exporter);
15            
16             sub new {
17 6     6 1 7653 my $type = shift;
18 6         16 my $in_type = shift;
19 6         16 my $input = shift;
20            
21             #
22             # Currently, new() accepts these two sets of parameters
23             # 1. new ('file'=>$tmpl_file)
24             # $tmpl_file is the path of the template file
25             # 2. new ('id'=> $tmpl_id)
26             # $tmpl_id is the template id
27             # This method is to hide the implementation of templates
28             # storage. Templates can be stored in file systems or in database.
29             # Currently, templates are stored in file systems. The root directory can be
30             # changed by calling Text::PORE::setTemplateRootDir($templateRootDir).
31             # The default root diretory is the current directory (".").
32             # Files are named as "$template_id.tpl".
33             #
34 6 100       26 if ($in_type eq 'file') {}
    50          
35             elsif ($in_type eq 'id') {
36 1         5 $input = Text::PORE::Globals::getTemplateRootDir() . "/$input.tpl";
37             }
38             else {
39 0         0 return undef;
40             }
41            
42 6         19 my ($self) = { };
43 6   33     44 bless $self, ref($type) || $type;
44            
45 6         43 $self->{'input'} = new FileHandle "< $input";
46            
47 6 50       519 if (!$self->{'input'}) {
48 0         0 carp "Cannot open file $input";
49 0         0 return undef;
50             }
51            
52 6         18 $self->{'filename'} = $input;
53            
54 6         159 $self->{'globals'} = $Text::PORE::Globals::globalVariables;
55            
56 6         21 $self;
57             }
58            
59            
60             # this indeirection is used to hide the method of input from the lexer..
61             # the lexer only needs to call readLine on it's input object, and allow
62             # the input object to get the data from wherever it desires
63             sub readLine {
64 50     50 0 65 my $self = shift;
65 50         53 my $return;
66            
67 50         1829 $return = $self->{'input'}->getline();
68            
69 50         2508 $return;
70             }
71            
72            
73             # parse registers the template object as the lexer's input object, then
74             # calls yyparse() to get a syntax tree. subsequent calls to parse are
75             # nop's, as the syntax tree would be the same, and it is already cached
76             sub parse {
77 6     6 0 21 my $self = shift;
78            
79 6 50       27 return 0 if $self->{'parsed'};
80            
81 6         30 setInput($self);
82            
83 6         139 $self->{'tree'} = yyparse();
84            
85             # TODO - should check isa('Node');
86 6 50       119 (ref $self->{'tree'} eq 'Text::PORE::Node::Queue') || return $self-{'tree'};
87            
88 6         14 $self->{'parsed'} = 1;
89            
90 6         25 return 0;
91             }
92            
93            
94             # render makes sure the template is parsed, registers an output object,
95             # then walks the syntax tree which resulted from it's parsing
96             sub render {
97 6     6 0 12 my $self = shift;
98 6         9 my $obj = shift;
99 6         146 my $output = shift;
100            
101 6         8 my $return;
102            
103             # make sure it's parsed, return if failure
104 6 50       24 ($return = $self->parse()) && return $return;
105            
106 6         42 setOutput Text::PORE::Node $output;
107            
108 6         44 $self->{'globals'}->LoadAttributes('_context' => $obj);
109            
110 6         30 $return = $self->{'tree'}->traverse($self->{'globals'});
111            
112 6         31 $self->{'globals'}->LoadAttributes('_context' => undef);
113            
114 6         31 return $return;
115             }
116            
117             sub setEnv {
118 0     0 0   my $self = shift;
119 0           my $arg = shift;
120            
121 0           $self->{'globals'}{'_env'} = {$self->{'globals'}{'_env'}, %$arg};
122             }
123            
124             sub unsetEnv {
125 0     0 0   my $self = shift;
126 0           my @args = @_;
127            
128 0           foreach (@args) {
129 0           delete $self->{'globals'}{'_env'}{$_};
130             }
131             }
132            
133             1;
134            
135             __END__