line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Template::Caribou; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
6
|
|
|
6
|
|
1919454
|
$Template::Caribou::AUTHORITY = 'cpan:YANICK'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
# ABSTRACT: class-based HTML-centric templating system |
6
|
|
|
|
|
|
|
$Template::Caribou::VERSION = '0.2.4'; |
7
|
|
|
|
|
|
|
|
8
|
6
|
|
|
6
|
|
51
|
use strict; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
202
|
|
9
|
6
|
|
|
6
|
|
95
|
use warnings; |
|
6
|
|
|
|
|
13
|
|
|
6
|
|
|
|
|
207
|
|
10
|
6
|
|
|
6
|
|
29
|
no warnings qw/ uninitialized /; |
|
6
|
|
|
|
|
12
|
|
|
6
|
|
|
|
|
216
|
|
11
|
|
|
|
|
|
|
|
12
|
6
|
|
|
6
|
|
34
|
use Carp; |
|
6
|
|
|
|
|
10
|
|
|
6
|
|
|
|
|
434
|
|
13
|
6
|
|
|
6
|
|
2528
|
use Moose::Role; |
|
6
|
|
|
|
|
13677
|
|
|
6
|
|
|
|
|
41
|
|
14
|
6
|
|
|
6
|
|
43223
|
use MooseX::SemiAffordanceAccessor; |
|
6
|
|
|
|
|
44774
|
|
|
6
|
|
|
|
|
40
|
|
15
|
6
|
|
|
6
|
|
49397
|
use MooseX::ClassAttribute; |
|
6
|
|
|
|
|
545462
|
|
|
6
|
|
|
|
|
43
|
|
16
|
6
|
|
|
6
|
|
1872628
|
use Template::Caribou::Utils; |
|
6
|
|
|
|
|
22
|
|
|
6
|
|
|
|
|
298
|
|
17
|
6
|
|
|
6
|
|
6079
|
use Path::Class qw/ file dir /; |
|
6
|
|
|
|
|
340765
|
|
|
6
|
|
|
|
|
561
|
|
18
|
6
|
|
|
6
|
|
10771
|
use Method::Signatures; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use Template::Caribou::Tags; |
21
|
|
|
|
|
|
|
use Moose::Exporter; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
Moose::Exporter->setup_import_methods( |
24
|
|
|
|
|
|
|
as_is => [ 'template', 'attr', 'show' ], |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
func template( $name, $code ) { |
28
|
|
|
|
|
|
|
my $class = caller(0); |
29
|
|
|
|
|
|
|
$class->set_template( $name => $code ); |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
use Moose::Util::TypeConstraints; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
role_type 'Formatter', { |
37
|
|
|
|
|
|
|
role => 'Template::Caribou::Formatter' |
38
|
|
|
|
|
|
|
}; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
coerce Formatter |
41
|
|
|
|
|
|
|
=> from 'Str' => via { |
42
|
|
|
|
|
|
|
s/^\+/Template::Caribou::Formatter::/; |
43
|
|
|
|
|
|
|
eval "use $_; 1" |
44
|
|
|
|
|
|
|
or die "couldn't load '$_': $@"; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$_->new; |
47
|
|
|
|
|
|
|
}; |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
has formatter => ( |
50
|
|
|
|
|
|
|
is => 'rw', |
51
|
|
|
|
|
|
|
does => 'Formatter', |
52
|
|
|
|
|
|
|
predicate => 'has_formatter', |
53
|
|
|
|
|
|
|
clearer => 'clear_formatter', |
54
|
|
|
|
|
|
|
handles => 'Template::Caribou::Formatter', |
55
|
|
|
|
|
|
|
coerce => 1, |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
method set_template($name,$value) { |
59
|
|
|
|
|
|
|
$self->meta->add_method( "template $name" => $value ); |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
method t($name) { |
63
|
|
|
|
|
|
|
my $method = $self->meta->find_method_by_name( "template $name" ) |
64
|
|
|
|
|
|
|
or die "template '$name' not found\n"; |
65
|
|
|
|
|
|
|
return $method->body; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
method all_templates { |
69
|
|
|
|
|
|
|
return |
70
|
|
|
|
|
|
|
sort |
71
|
|
|
|
|
|
|
map { /\s(.*)/ } |
72
|
|
|
|
|
|
|
grep { /^template / } $self->meta->get_method_list; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
method import_template_dir($directory) { |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
$directory = dir( $directory ); |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
return map { |
82
|
|
|
|
|
|
|
$self->import_template("$_") |
83
|
|
|
|
|
|
|
} grep { $_->basename =~ /\.bou$/ } grep { -f $_ } $directory->children; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub add_template { |
87
|
|
|
|
|
|
|
my ( $self, $label, $sub ) = @_; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
$self->set_template( $label => $sub ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub render { |
93
|
|
|
|
|
|
|
my ( $self, $template, @args ) = @_; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
my $method = ref $template eq 'CODE' ? $template : $self->t($template); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
my $output = do |
98
|
|
|
|
|
|
|
{ |
99
|
|
|
|
|
|
|
local $Template::Caribou::TEMPLATE = $self; |
100
|
|
|
|
|
|
|
#$Template::Caribou::TEMPLATE || $self; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
local $Template::Caribou::IN_RENDER = 1; |
103
|
|
|
|
|
|
|
local *STDOUT; |
104
|
|
|
|
|
|
|
local *::RAW; |
105
|
|
|
|
|
|
|
local $Template::Caribou::OUTPUT; |
106
|
|
|
|
|
|
|
local %Template::Caribou::attr; |
107
|
|
|
|
|
|
|
tie *STDOUT, 'Template::Caribou::Output'; |
108
|
|
|
|
|
|
|
tie *::RAW, 'Template::Caribou::OutputRaw'; |
109
|
|
|
|
|
|
|
select STDOUT; |
110
|
|
|
|
|
|
|
my $res = $method->( $self, @args ); |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
$Template::Caribou::OUTPUT |
113
|
|
|
|
|
|
|
or ref $res ? $res : Template::Caribou::Output::escape( $res ); |
114
|
|
|
|
|
|
|
}; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# if we are still within a render, we turn the string |
117
|
|
|
|
|
|
|
# into an object to say "don't touch" |
118
|
|
|
|
|
|
|
$output = Template::Caribou::String->new( $output ) |
119
|
|
|
|
|
|
|
if $Template::Caribou::IN_RENDER; |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
print ::RAW $output if $Template::Caribou::IN_RENDER and not defined wantarray; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
if( !$Template::Caribou::IN_RENDER and $self->has_formatter ) { |
124
|
|
|
|
|
|
|
$output = $self->format($output); |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
return $output; |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub show { |
132
|
|
|
|
|
|
|
croak "'show()' must be called from within a template" |
133
|
|
|
|
|
|
|
unless $Template::Caribou::IN_RENDER; |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
print ::RAW $Template::Caribou::TEMPLATE->render( @_ ); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
__END__ |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=pod |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=encoding UTF-8 |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 NAME |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Template::Caribou - class-based HTML-centric templating system |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 VERSION |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
version 0.2.4 |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 SYNOPSIS |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
package MyTemplate; |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
use Moose; |
159
|
|
|
|
|
|
|
use Template::Caribou; |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
with 'Template::Caribou'; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
use Template::Caribou::Tags::HTML qw/ :all /; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
has name => ( is => 'ro' ); |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
template page => sub { |
168
|
|
|
|
|
|
|
html { |
169
|
|
|
|
|
|
|
head { title { 'Example' } }; |
170
|
|
|
|
|
|
|
show( 'body' ); |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
}; |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
template body => sub { |
175
|
|
|
|
|
|
|
my $self = shift; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
body { |
178
|
|
|
|
|
|
|
h1 { 'howdie ' . $self->name } |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
}; |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
package main; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
my $template = MyTemplate->new( name => 'Yanick' ); |
185
|
|
|
|
|
|
|
print $template->render('page'); |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 DESCRIPTION |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
WARNING: Codebase is alpha with extreme prejudice. Assume that bugs are |
190
|
|
|
|
|
|
|
teeming and that the API is subject to change. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
L<Template::Caribou> is a L<Moose>-based, class-centric templating system |
193
|
|
|
|
|
|
|
mostly aimed at producing sgml-like outputs, mostly HTML, but also XML, SVG, etc. It is |
194
|
|
|
|
|
|
|
heavily inspired by L<Template::Declare>. |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
For a manual on how to use C<Template::Caribou>, have a peek at |
197
|
|
|
|
|
|
|
L<Template::Caribou::Manual>. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 METHODS |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 pretty_render() |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Returns true if rendered templates are passed through the prettifier. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head2 enable_pretty_render( $bool ) |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
if set to true, rendered templates will be filtered by a prettifier |
208
|
|
|
|
|
|
|
before being returned by the C<render()> method. |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head2 import_template_dir( $directory ) |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Imports all the files with a C<.bou> extension in I<$directory> |
213
|
|
|
|
|
|
|
as templates (non-recursively). |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Returns the name of the imported templates. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head2 show( $template, @args ) |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Outside of a template, behaves like C<render()>. In a template, prints out |
220
|
|
|
|
|
|
|
the result of the rendering in addition of returning it. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=head1 SEE ALSO |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
L<http://babyl.dyndns.org/techblog/entry/caribou> - The original blog entry |
225
|
|
|
|
|
|
|
introducing L<Template::Caribou>. |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
L<Template::Declare> |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head1 AUTHOR |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Yanick Champoux <yanick@cpan.org> |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
This software is copyright (c) 2014 by Yanick Champoux. |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
238
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=cut |