line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer::Template::Caribou; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:YANICK'; |
3
|
|
|
|
|
|
|
#ABSTRACT: Template::Caribou wrapper for Dancer |
4
|
|
|
|
|
|
|
$Dancer::Template::Caribou::VERSION = '1.0.1'; |
5
|
2
|
|
|
2
|
|
2779
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
48
|
|
6
|
2
|
|
|
2
|
|
6
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
51
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
6
|
use Moose::Util qw/ with_traits find_meta /; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
21
|
|
9
|
2
|
|
|
2
|
|
566
|
use Dancer::Config qw/ setting /; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
91
|
|
10
|
2
|
|
|
2
|
|
8
|
use Module::Runtime qw/ use_module /; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
13
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
861
|
use Moo; |
|
2
|
|
|
|
|
19555
|
|
|
2
|
|
|
|
|
6
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
extends 'Dancer::Template::Abstract'; |
15
|
|
|
|
|
|
|
|
16
|
0
|
|
|
0
|
|
0
|
sub _build_name { 'Dancer::Template::Caribou' }; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
has 'default_tmpl_ext' => ( |
19
|
|
|
|
|
|
|
is => 'ro', |
20
|
|
|
|
|
|
|
default => sub { 'bou' }, |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
has default_template => ( |
24
|
|
|
|
|
|
|
is => 'ro', |
25
|
|
|
|
|
|
|
lazy => 1, |
26
|
|
|
|
|
|
|
default => sub { |
27
|
|
|
|
|
|
|
$_[0]->config->{default_template} || 'page'; |
28
|
|
|
|
|
|
|
}, |
29
|
|
|
|
|
|
|
); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
has default_layout_template => ( |
32
|
|
|
|
|
|
|
is => 'ro', |
33
|
|
|
|
|
|
|
lazy => 1, |
34
|
|
|
|
|
|
|
default => sub { |
35
|
|
|
|
|
|
|
$_[0]->config->{default_layout_template} || 'page'; |
36
|
|
|
|
|
|
|
}, |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has namespace => ( |
40
|
|
|
|
|
|
|
is => 'ro', |
41
|
|
|
|
|
|
|
lazy => 1, |
42
|
|
|
|
|
|
|
predicate => 'has_namespace', |
43
|
|
|
|
|
|
|
default => sub { |
44
|
|
|
|
|
|
|
$_[0]->config->{namespace} || 'Dancer::View'; |
45
|
|
|
|
|
|
|
}, |
46
|
|
|
|
|
|
|
); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
has layout_namespace => ( |
49
|
|
|
|
|
|
|
is => 'ro', |
50
|
|
|
|
|
|
|
lazy => 1, |
51
|
|
|
|
|
|
|
default => sub { |
52
|
|
|
|
|
|
|
$_[0]->config->{layout_namespace} |
53
|
|
|
|
|
|
|
or $_[0]->namespace . '::Layout'; |
54
|
|
|
|
|
|
|
}, |
55
|
|
|
|
|
|
|
); |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub apply_layout { |
58
|
3
|
|
|
3
|
0
|
162
|
return $_[1]; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub layout { |
62
|
0
|
|
|
0
|
1
|
0
|
return $_[3]; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
sub render { |
66
|
3
|
|
|
3
|
1
|
193
|
my( $self, $template, $tokens ) = @_; |
67
|
|
|
|
|
|
|
|
68
|
3
|
|
|
|
|
4
|
my $class = $template; |
69
|
3
|
100
|
|
|
|
40
|
$class = join '::', $self->namespace, $class |
70
|
|
|
|
|
|
|
unless $class =~ s/^\+//; |
71
|
|
|
|
|
|
|
|
72
|
3
|
|
|
|
|
22
|
use_module($class); |
73
|
|
|
|
|
|
|
|
74
|
3
|
|
|
|
|
111
|
my $method = $self->default_template; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# TODO build a cache of layout + class classes? |
77
|
3
|
100
|
|
|
|
35
|
if ( my $layout = Dancer::App->current->setting('layout') ) { |
78
|
1
|
|
|
|
|
15
|
my $layout_class = $layout; |
79
|
1
|
50
|
|
|
|
5
|
$layout_class = join '::', $self->layout_namespace, $layout_class |
80
|
|
|
|
|
|
|
unless $layout_class =~ s/^\+//; |
81
|
|
|
|
|
|
|
|
82
|
1
|
|
|
|
|
4
|
$class = with_traits( $class, use_module($layout_class) ); |
83
|
1
|
|
|
|
|
7313
|
$method = $self->default_layout_template; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
3
|
|
|
|
|
120
|
my $x = $class->new( %$tokens)->$method; |
87
|
2
|
|
|
2
|
|
8902
|
use utf8;utf8::decode($x); |
|
2
|
|
|
|
|
16
|
|
|
2
|
|
|
|
|
9
|
|
|
3
|
|
|
|
|
1975
|
|
88
|
3
|
|
|
|
|
248
|
return $x; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
sub view { |
92
|
6
|
|
|
6
|
1
|
13490
|
my( $self, $view ) = @_; |
93
|
6
|
|
|
|
|
13
|
return $view; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# TODO check if the class exists |
97
|
|
|
|
|
|
|
sub view_exists { |
98
|
3
|
|
|
3
|
1
|
12
|
1; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__END__ |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=pod |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=encoding UTF-8 |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 NAME |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Dancer::Template::Caribou - Template::Caribou wrapper for Dancer |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 VERSION |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
version 1.0.1 |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 SYNOPSIS |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# in 'config.yml' |
121
|
|
|
|
|
|
|
template: Caribou |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
engines: |
124
|
|
|
|
|
|
|
Caribou: |
125
|
|
|
|
|
|
|
namespace: MyApp::View |
126
|
|
|
|
|
|
|
layout_namespace: MyApp::View::Layout |
127
|
|
|
|
|
|
|
default_template: inner_page |
128
|
|
|
|
|
|
|
default_layout_template: page |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
# and then in the application |
131
|
|
|
|
|
|
|
get '/' => sub { |
132
|
|
|
|
|
|
|
...; |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
template 'main' => \%options; |
135
|
|
|
|
|
|
|
}; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 DESCRIPTION |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
C<Dancer::Template::Caribou> is an interface for the L<Template::Caribou> |
140
|
|
|
|
|
|
|
template system. Be forewarned, both this module and C<Template::Caribou> |
141
|
|
|
|
|
|
|
itself are alpha-quality software and are still subject to any changes. |
142
|
|
|
|
|
|
|
B<Caveat Maxima Emptor>. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 Basic Usage |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
At the base, if you do |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
get '/' => sub { |
149
|
|
|
|
|
|
|
... |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
return template 'MyView', \%options; |
152
|
|
|
|
|
|
|
}; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
the template name (here I<MyView>) will be concatenated with the |
155
|
|
|
|
|
|
|
configured view namespace (which defaults to I<Dancer::View>) |
156
|
|
|
|
|
|
|
to generate the Caribou class name. A Caribou object is created |
157
|
|
|
|
|
|
|
using C<%options> as its arguments, and its default template (defaulting to C<page>) |
158
|
|
|
|
|
|
|
is then |
159
|
|
|
|
|
|
|
rendered. In other words, the last line of the code above becomes |
160
|
|
|
|
|
|
|
equivalent to |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
return Dancer::View::MyView->new( %options )->page; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head3 Layouts as roles |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
Layouts, just like templates, are package names. They are expected to be |
167
|
|
|
|
|
|
|
roles that will be composed with the template class. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head1 CONFIGURATION |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=over |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item default_template |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
The name of the entry template to use. In other words, with the configuration |
176
|
|
|
|
|
|
|
given in the SYNOPSIS, the dancer code |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
return template 'MyThing'; |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
is equivalent to |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
return MyApp::View::MyThing->page; |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Defaults to C<page>. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item default_layout_template |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Entry template to use when a layout is provided. Defaults to C<page>. |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=item namespace |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
The namespace under which the Caribou template classes are. |
193
|
|
|
|
|
|
|
defaults to C<Dancer::View>. |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Template names can be prefixed with a plus sign if you want it to be used as an absolute namespace. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
template 'Relative::View'; # -> Dancer::View::Relative::View |
198
|
|
|
|
|
|
|
template '+My::Absolute::View'; # -> My::Absolute::View |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
=item layout_namespace |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
The namespace under which the Caribou layout roles are. |
203
|
|
|
|
|
|
|
defaults to the C<::Layout> sub-namespace under the template |
204
|
|
|
|
|
|
|
namespace. |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
Like template names, layout names can be prefixed with a plus sign for |
207
|
|
|
|
|
|
|
absolute namespaces; |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
set layout => 'My::Relative'; # -> Dancer::View::Layour::My::Relative |
210
|
|
|
|
|
|
|
set layout => '+My::Absolute'; # -> My::Absolute |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
=back |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=head1 AUTHOR |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
Yanick Champoux <yanick@babyl.dyndns.org> |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
This software is copyright (c) 2013 by Yanick Champoux. |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
223
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=cut |