| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Bricklayer::Templater; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
8021
|
use Bricklayer::Templater::Sequencer; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
41
|
|
|
4
|
1
|
|
|
1
|
|
9
|
use Carp; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
1518
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Bricklayer::Templater - yet another templating system. Pure perl, highly flexible |
|
9
|
|
|
|
|
|
|
with very few dependencies. |
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Bricklayer::Templater; |
|
14
|
|
|
|
|
|
|
use Cwd; |
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
my $cwd = cwd(); |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# create a new templater with a context object and a working directory |
|
19
|
|
|
|
|
|
|
my $t = Bricklayer::Templater->new($context, $cwd); |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# run the templater on a named template |
|
22
|
|
|
|
|
|
|
$t->run_templater('name_of_template'); |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# retrieve the page after running templater on it. |
|
25
|
|
|
|
|
|
|
my $page = $t->_page(); |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Bricklayer::Templater began as a way to make a simple easy to use flexible |
|
30
|
|
|
|
|
|
|
templating engine. It has evolved over time but still retains that flexibility, |
|
31
|
|
|
|
|
|
|
simplicity, and ease of use. |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
It is based on template tags and is completely configurable as far as how those |
|
34
|
|
|
|
|
|
|
tags are identified. The default is |
|
35
|
|
|
|
|
|
|
you can specify different start and end brackets and identifiers (the BK in the above tags) |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head2 Configuring Templater options |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Changing start_bracket for the template objects tags |
|
40
|
|
|
|
|
|
|
$t->start_bracket('['); #default is < |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
Change the end_bracket for the template objects tags |
|
43
|
|
|
|
|
|
|
$t->end_bracket(']'); #default is > |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
Change the identifier for the templater objects tags |
|
46
|
|
|
|
|
|
|
$t->identifier('?'); #default is BK |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
Change the template extension |
|
49
|
|
|
|
|
|
|
$t->ext('tmpl'); #default is txml |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
There are two primary purposes for this configurability. One is to for aesthetic |
|
52
|
|
|
|
|
|
|
reasons, the other is for multipass templating. Multipass templating is possible |
|
53
|
|
|
|
|
|
|
by running the template once for one configuration of tags then again on the results |
|
54
|
|
|
|
|
|
|
with a different configuration of tags. |
|
55
|
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=head2 Running a Template |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
There are two ways you can run a template. The first and easiest is to call |
|
59
|
|
|
|
|
|
|
$t->run_templater('template_name'); This will look in your working directory |
|
60
|
|
|
|
|
|
|
for a template by that name and with the configured extension and then run it. |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
The template will be stored in $t->_page() or be published with the publish hook |
|
63
|
|
|
|
|
|
|
provided by you if you sub classed the engine. |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head2 The publish method. |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
There is one method you probably want to override if you subclass this engine. |
|
68
|
|
|
|
|
|
|
publish() This method will be called by handlers with their results. If you |
|
69
|
|
|
|
|
|
|
don't override it then the default is to append those results to the _page attribute |
|
70
|
|
|
|
|
|
|
of the template object. |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 The rest of the API |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=cut |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
$VERSION='0.9.8'; |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head3 new |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
Initializes a Templater object. Requires a context and working directory as the first two arguments. |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=cut |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub new { |
|
85
|
3
|
100
|
|
3
|
1
|
1372
|
do {carp($_[0]." Requires a working directory"); return; } unless defined $_[2]; |
|
|
1
|
|
|
|
|
380
|
|
|
|
1
|
|
|
|
|
207
|
|
|
86
|
2
|
100
|
|
|
|
9
|
do {carp($_[0]." Requires a context"); return; } unless defined $_[1]; |
|
|
1
|
|
|
|
|
285
|
|
|
|
1
|
|
|
|
|
9
|
|
|
87
|
1
|
|
|
|
|
8
|
my $obj = bless({App => $_[1], WD => $_[2]}, $_[0]); |
|
88
|
|
|
|
|
|
|
|
|
89
|
1
|
|
|
|
|
8
|
$obj->ext('txml'); |
|
90
|
1
|
|
|
|
|
4
|
$obj->start_bracket('<'); |
|
91
|
1
|
|
|
|
|
4
|
$obj->end_bracket('>'); |
|
92
|
1
|
|
|
|
|
4
|
$obj->identifier('BK'); |
|
93
|
1
|
|
|
|
|
11
|
return $obj; |
|
94
|
|
|
|
|
|
|
} |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head3 load_template_file |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
my $file = $t->load_template_file('template_name') loads a template file from the working directory |
|
99
|
|
|
|
|
|
|
there are two ways to specify the template name. |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head4 path/name syntax |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
$t->load_template_file('relative/path/template_name') |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head4 name::space syntax (perl like) |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
$t->load_template_file('name::space::template_name') |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub load_template_file { |
|
112
|
3
|
|
|
3
|
1
|
490
|
my $self = shift; |
|
113
|
3
|
|
|
|
|
7
|
my $filename = shift; |
|
114
|
3
|
|
|
|
|
8
|
my $extension = $self->ext(); |
|
115
|
3
|
|
|
|
|
37
|
my $TemplateFile = $self->WD()."/templates/".$filename; |
|
116
|
3
|
|
|
|
|
7
|
$TemplateFile .= ".$extension"; |
|
117
|
3
|
|
|
|
|
10
|
$TemplateFile =~ s/::/\//g; # use double colon to indicate template directory seperators |
|
118
|
3
|
|
|
|
|
4
|
my $TemplateObj; |
|
119
|
|
|
|
|
|
|
my $Template; |
|
120
|
3
|
|
|
|
|
379
|
carp("loading $TemplateFile"); |
|
121
|
3
|
50
|
|
|
|
544
|
open( TEMPLATE, $TemplateFile ) |
|
122
|
|
|
|
|
|
|
or croak("Cannot open Template File: $TemplateFile "); |
|
123
|
|
|
|
|
|
|
|
|
124
|
3
|
|
|
|
|
86
|
while ( read( TEMPLATE, my $line, 1000 ) ) { |
|
125
|
3
|
|
|
|
|
12
|
$Template .= $line; |
|
126
|
|
|
|
|
|
|
} |
|
127
|
3
|
|
|
|
|
7
|
chomp $Template; |
|
128
|
3
|
|
|
|
|
36
|
close TEMPLATE; |
|
129
|
3
|
|
|
|
|
10
|
$self->_template($Template); |
|
130
|
3
|
|
|
|
|
14
|
return $Template; |
|
131
|
|
|
|
|
|
|
} |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head3 $t->run_templater($file, $params) |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
run_templater runs the sequencer on the text in $filename. The results |
|
136
|
|
|
|
|
|
|
of the template run will be stored wherever publish() puts it. |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=cut |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
sub run_templater { |
|
141
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
|
142
|
1
|
|
|
|
|
1
|
my $filename = shift; |
|
143
|
1
|
|
|
|
|
2
|
my $Params = shift; |
|
144
|
1
|
50
|
|
|
|
4
|
$self->load_template_file($filename) |
|
145
|
|
|
|
|
|
|
or croak('Failed to loadi ['. $filename. '] template'); |
|
146
|
1
|
|
|
|
|
3
|
$self->run_sequencer($self->_template, $Params); |
|
147
|
1
|
|
|
|
|
2
|
return 1; |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head3 $t->run_sequencer($text, $params) |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
run_sequencer runs the sequencer on the text in $text. The results |
|
153
|
|
|
|
|
|
|
of the template run will be stored wherever publish() puts it. |
|
154
|
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=cut |
|
156
|
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
sub run_sequencer { |
|
158
|
2
|
|
|
2
|
1
|
6
|
my $self = shift; |
|
159
|
2
|
|
|
|
|
3
|
my $Template = shift; |
|
160
|
2
|
|
|
|
|
8
|
my $tagID = $self->identifier(); |
|
161
|
2
|
|
|
|
|
5
|
my $Params = shift; |
|
162
|
2
|
|
|
|
|
4
|
my $handler_loc = $self->{WD}; |
|
163
|
2
|
|
|
|
|
7
|
my $TemplateObj = Bricklayer::Templater::Sequencer->new_sequencer($Template, $tagID, $self->start_bracket, $self->end_bracket); |
|
164
|
2
|
|
|
|
|
8
|
my $ParsedPage = $TemplateObj->return_parsed($self, $Params, $handler_loc); |
|
165
|
2
|
|
|
|
|
13
|
return; |
|
166
|
|
|
|
|
|
|
} |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head3 publish |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
default publish callback. You'll probably be overriding this but if you don't then the handlers will use this method |
|
171
|
|
|
|
|
|
|
to store the result of the parsed page in the $templater->{_page} attribute. |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=cut |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
sub publish { |
|
176
|
4
|
|
|
4
|
1
|
5
|
my $self = shift; |
|
177
|
4
|
|
|
|
|
5
|
my $stuff = shift; |
|
178
|
4
|
100
|
|
|
|
37
|
$self->{_page} .= $stuff if defined $stuff; |
|
179
|
|
|
|
|
|
|
} |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head3 clear |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
$t->clear() Clears the contents of _page() it's a convenience method. If you override |
|
184
|
|
|
|
|
|
|
the publish method you might want to override this one too if you need it. |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=cut |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub clear { |
|
189
|
3
|
|
|
3
|
1
|
579
|
$self = shift; |
|
190
|
3
|
|
|
|
|
10
|
$self->{_page} = undef; |
|
191
|
|
|
|
|
|
|
} |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head3 start_bracket |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sets and returns the start_bracket attribute for the templater template tags |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub start_bracket { |
|
200
|
4
|
|
|
4
|
1
|
9
|
my $self = shift; |
|
201
|
4
|
|
|
|
|
8
|
my $var = shift; |
|
202
|
4
|
100
|
|
|
|
15
|
$self->{start_bracket} = $var if $var; |
|
203
|
4
|
|
|
|
|
17
|
return $self->{start_bracket}; |
|
204
|
|
|
|
|
|
|
} |
|
205
|
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head3 end_bracket |
|
207
|
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sets and returns the end_bracket attribute for the templater template tags |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |
|
211
|
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub end_bracket { |
|
213
|
4
|
|
|
4
|
1
|
13
|
my $self = shift; |
|
214
|
4
|
|
|
|
|
5
|
my $var = shift; |
|
215
|
4
|
100
|
|
|
|
11
|
$self->{end_bracket} = $var if $var; |
|
216
|
4
|
|
|
|
|
26
|
return $self->{end_bracket}; |
|
217
|
|
|
|
|
|
|
} |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
=head3 ext |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sets and returns the ext attribute for the templater otherwise known as the template file extension |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=cut |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
sub ext { |
|
226
|
5
|
|
|
5
|
1
|
9
|
my $self = shift; |
|
227
|
5
|
|
|
|
|
10
|
my $var = shift; |
|
228
|
5
|
100
|
|
|
|
35
|
$self->{ext} = $var if $var; |
|
229
|
5
|
|
|
|
|
18
|
return $self->{ext}; |
|
230
|
|
|
|
|
|
|
} |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head3 identifier |
|
233
|
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
sets and returns the identifier attribute for the templater otherwise known as the tag identifier |
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=cut |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
sub identifier { |
|
239
|
4
|
|
|
4
|
1
|
8
|
my $self = shift; |
|
240
|
4
|
|
|
|
|
6
|
my $var = shift; |
|
241
|
4
|
100
|
|
|
|
11
|
$self->{identifier} = $var if $var; |
|
242
|
4
|
|
|
|
|
11
|
return $self->{identifier}; |
|
243
|
|
|
|
|
|
|
} |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head3 _template |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
sets and returns the _template attribute for the templater a sort of scratchpad that the templater uses to store templates |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=cut |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
sub _template { |
|
252
|
5
|
|
|
5
|
|
7
|
my $self = shift; |
|
253
|
5
|
|
|
|
|
8
|
my $var = shift; |
|
254
|
5
|
100
|
|
|
|
15
|
$self->{template} = $var if $var; |
|
255
|
5
|
|
|
|
|
13
|
return $self->{template}; |
|
256
|
|
|
|
|
|
|
} |
|
257
|
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=head3 _page |
|
259
|
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
sets and returns the _page attribute for the templater where the default publish callback stores the return |
|
261
|
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=cut |
|
263
|
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
sub _page { |
|
265
|
3
|
|
|
3
|
|
8
|
my $self = shift; |
|
266
|
3
|
|
|
|
|
3
|
my $var = shift; |
|
267
|
3
|
50
|
|
|
|
6
|
$self->{_page} = $var if $var; |
|
268
|
3
|
|
|
|
|
14
|
return $self->{_page}; |
|
269
|
|
|
|
|
|
|
} |
|
270
|
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=head3 app |
|
272
|
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
sets and returns the app attribute for the templater otherwise known as the context |
|
274
|
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=cut |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
sub app { |
|
278
|
1
|
|
|
1
|
1
|
7
|
return $_[0]->{App}; |
|
279
|
|
|
|
|
|
|
} |
|
280
|
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
=head3 WD |
|
282
|
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
sets and returns the WD attribute for the templater otherwise known as the working directory |
|
284
|
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=cut |
|
286
|
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
sub WD { |
|
288
|
4
|
|
|
4
|
1
|
2613
|
return $_[0]->{WD}; |
|
289
|
|
|
|
|
|
|
} |
|
290
|
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
return 1; |
|
292
|
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=head1 Authors |
|
294
|
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
Jeremy A. Wall |
|
296
|
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=head1 BUGS |
|
298
|
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
Like any Module of sufficient complexity there are probably some things I missed. |
|
300
|
|
|
|
|
|
|
See http://rt.cpan.org to report and view bugs |
|
301
|
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
303
|
|
|
|
|
|
|
(C) Copyright 2007 Jeremy Wall |
|
304
|
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
This program is free software you can redistribute it and/or modify it under the same terms as Perl itself. |
|
306
|
|
|
|
|
|
|
|
|
307
|
|
|
|
|
|
|
See http://www.Perl.com/perl/misc/Artistic.html |
|
308
|
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=cut |