lib/Dancer/Template/Caribou.pm

filter: Statementstmt 91.8% 102 / 111 Branchbran 50.0% 10 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.3% 28 / 31 Podpod 50.0% 4 / 8 totaltotal 84.7% 144 / 170 CC 13 SCAR 8.6
CC
13
Cov
85%
render
7.2
generate_view_class
4.0
generate_layout_class
3.0
CRAP 2.4
« Build ↑ Summary
1 package Dancer::Template::Caribou;
2 BEGIN {
3 2 ▶ $Dancer::Template::Caribou::AUTHORITY = 'cpan:YANICK';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 {
6 1 $Dancer::Template::Caribou::VERSION = '0.1.1';
7 }
8 #ABSTRACT: Template::Caribou wrapper for Dancer
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Path::Iterator::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Moose::Util qw/ with_traits find_meta /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Dancer::Config qw/ setting /;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 extends 'Dancer::Template::Abstract';
22  
23 0 ▶sub _build_name { 'Dancer::Template::Caribou' };
Subroutine _build_name: not called CC=1 SCAR=6.9
24  
25 has 'default_tmpl_ext' => (
26 is => 'ro',
27 1 ▶ default => sub { 'bou' },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 );
29  
30 has view_class => (
31 is => 'ro',
32 1 ▶ default => sub { { } },
Subroutine __ANON__: called CC=1 SCAR=0.0
33 1 );
34  
35 has layout_class => (
36 is => 'ro',
37 1 ▶ default => sub { { } },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 1 );
39  
40 has namespace => (
41 is => 'ro',
42 lazy => 1,
43 default => sub {
44 1 ▶ $_[0]->config->{namespace} || 'Dancer::View';
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->config->{"namespace"} or "Dancer::View" 1 0 1
45 },
46 1 );
47  
48 sub BUILD {
49 1 ▶ my $self = shift;
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
50  
51 1 my $views_dir = setting 'views';
52  
53 1 my @views =
54 Path::Iterator::Rule->new->skip_dirs('layouts')->file->name('bou')->all(
55 $views_dir );
56  
57 1 $self->generate_view_class( $_ ) for @views;
58  
59 1 my @layouts =
60 Path::Iterator::Rule->new->file->name('bou')->all(
61 path( $views_dir, 'layouts' ) );
62  
63 1 $self->generate_layout_class( $_ ) for @layouts;
64 }
65  
66 sub generate_layout_class {
67 1 ▶ my( $self, $bou ) = @_;
Subroutine generate_layout_class: called CC=3 SCAR=11.0
Pod undocumented
68  
69 1 my $bou_dir = path($bou)->parent;
70 1 my $segment = ''.path($bou)->relative( setting( 'views').'/layouts')->parent;
71  
72 1 ( my $name = $segment ) =~ s#/#::#;
73 1 $name = join '::', $self->namespace, $name;
74  
75 1 my $inner = path($bou)->slurp;
76  
77 1 ▶ eval qq{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless find_meta($name) 1 0 1
78 package $name;
79  
80 use Moose::Role;
81 use Template::Caribou;
82  
83 with 'Template::Caribou';
84  
85 with 'Template::Caribou::Files' => {
86 dirs => [ '$bou_dir' ],
87 auto_reload => 1,
88 };
89  
90 # line 1 "$bou"
91  
92 $inner
93  
94 1;
95 } unless find_meta( $name );
96  
97 1 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 0 1
98  
99 1 $self->layout_class->{$segment} = $name;
100  
101 }
102  
103 sub generate_view_class {
104 3 ▶ my( $self, $bou ) = @_;
Subroutine generate_view_class: called CC=4 SCAR=13.9
Pod undocumented
105  
106 3 my $bou_dir = path($bou)->parent;
107 3 my $segment = ''.path($bou)->relative(setting('views'))->parent;
108  
109 3 ( my $name = $segment ) =~ s#/#::#;
110 3 $name = join '::', $self->namespace, $name;
111  
112 3 ▶ return if $self->layout_class->{$segment};
Branch: 1 branch
BranchTrueFalseTotal
if $self->layout_class->{$segment} 0 3 3
113  
114 3 my $inner = path($bou)->slurp;
115  
116 1 ▶ eval qq{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
unless find_meta($name) 3 0 3
117 package $name;
118  
119 use Moose;
120 use Template::Caribou;
121  
122 with 'Template::Caribou';
123  
124 with 'Template::Caribou::Files' => {
125 dirs => [ '$bou_dir' ],
126 auto_reload => 1,
127 };
128  
129 has app => (
130 is => 'ro',
131 handles => [ 'config' ],
132 );
133  
134 # line 1 "$bou"
135  
136 $inner
137  
138 1;
139 } unless find_meta($name);
140  
141 4 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 3 0 3
142  
143 4 $self->view_class->{$segment} = $name;
144  
145 }
146  
147 sub apply_layout {
148 1 ▶ return $_[1];
Subroutine apply_layout: not called CC=1 SCAR=0.0
Pod undocumented
149 }
150  
151 sub layout {
152 1 ▶ return $_[3];
Subroutine layout: not called CC=1 SCAR=0.0
Pod documented
153 }
154  
155 sub render {
156 4 ▶ my( $self, $template, $tokens ) = @_;
Subroutine render: called CC=5 SCAR=19.7
Pod documented
157  
158 4 $template =~ s/\.bou$//;
159  
160 4 my $class = $self->view_class->{$template};
161  
162 3 ▶ unless ( $class ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class) 0 3 3
163 0 my $c = $template;
164 0 $c =~ s#/#::#g;
165 0 $c = join '::', $self->namespace, $c;
166 die "template '$template' not found\n"
167 0 ▶ unless eval { $c->DOES('Template::Caribou') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $c->DOES("Template::Caribou") } } 0 0 0
168 0 $class = $c;
169 }
170  
171 3 ▶ if ( my $lay = Dancer::App->current->setting('layout') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lay = "Dancer::App"->current->setting("layout")) 1 2 3
172 1 ▶ my $role = $self->layout_class->{$lay}
Branch: 1 branch
BranchTrueFalseTotal
my $role = $self->layout_class->{$lay} or die "layout '${lay}' not defined\n" 0 1 1
173 or die "layout '$lay' not defined\n";
174  
175 1 $class = with_traits( $class, $role,
176 )
177 }
178  
179 3 my $x = $class->new( %$tokens)->render('page');
180 2 ▶ use utf8;utf8::decode($x);
Subroutine BEGIN: called CC=1 SCAR=0.2
181 0 return $x;
182 }
183  
184 sub view {
185 6 ▶ my( $self, $view ) = @_;
Subroutine view: called CC=1 SCAR=0.0
Pod documented
186 6 return $view;
187 }
188  
189 sub view_exists {
190 3 ▶ 1;
Subroutine view_exists: called CC=1 SCAR=0.0
Pod documented
191 }
192  
193  
194 1 1;
195  
196 __END__
197  
198 =pod
199  
200 =head1 NAME
201  
202 Dancer::Template::Caribou - Template::Caribou wrapper for Dancer
203  
204 =head1 VERSION
205  
206 version 0.1.1
207  
208 =head1 SYNOPSIS
209  
210 # in 'config.yml'
211 template: Caribou
212  
213 engines:
214 template:
215 Caribou:
216 namespace: MyApp::View
217 auto_reload: 1
218  
219  
220 # and then in the application
221 get '/' => sub {
222 ...;
223  
224 template 'main' => \%options;
225 };
226  
227 =head1 DESCRIPTION
228  
229 C<Dancer::Template::Caribou> is an interface for the L<Template::Caribou>
230 template system. Be forewarned, both this module and C<Template::Caribou>
231 itself are alpha-quality software and are still subject to any changes. <Caveat
232 Maxima Emptor>.
233  
234 =head2 Basic Usage
235  
236 At the base, if you do
237  
238 get '/' => sub {
239 ...
240  
241 return template 'MyView', \%options;
242 };
243  
244 the template name (here I<MyView>) will be concatenated with the
245 configured view namespace (which defaults to I<Dancer::View>)
246 to generate the Caribou class name. A Caribou object is created
247 using C<%options> as its arguments, and its inner template C<page> is then
248 rendered. In other words, the last line of the code above becomes
249 equivalent to
250  
251 return Dancer::View::MyView->new( %options )->render('page');
252  
253 =head2 '/views' template classes
254  
255 Template classes can be created straight from the C</views> directory.
256 Any directory containing a file named C<bou> will be turned into a
257 C<Template::Caribou> class. Additionally, any file with a C<.bou> extension
258 contained within that directory will be turned into a inner template for
259 that class.
260  
261 =head3 The 'bou' file
262  
263 The 'bou' file holds the custom bits of the Template::Caribou class.
264  
265 For example, a basic welcome template could be:
266  
267 # in /views/welcome/bou
268
269 use Template::Caribou::Tags::HTML ':all';
270  
271 has name => ( is => 'ro' );
272  
273 template page => sub {
274 my $self = shift;
275  
276 html {
277 head { title { 'My App' } };
278 body {
279 h1 { 'hello ' . $self->name .'!' };
280 };
281 }
282 };
283  
284 which would be invoqued via
285  
286 get '/hi/:name' => sub {
287 template 'welcome' => { name => param('name') };
288 };
289  
290 =head3 The inner template files
291  
292 All files with a '.bou' extension found in the same directory as the 'bou'
293 file become inner templates for the class. So, to continue with the example
294 above, we could change it into
295  
296 # in /views/howdie/bou
297
298 use Template::Caribou::Tags::HTML ':all';
299  
300 has name => ( is => 'ro' );
301  
302  
303 # in /views/howdie/page
304 html {
305 head { title { 'My App' } };
306 body {
307 h1 { 'howdie ' . $self->name . '!' };
308 };
309 }
310  
311 =head3 Layouts as roles
312  
313 For the layout sub-directory, an additional piece of magic is performed.
314 The 'bou'-marked directories are turned into roles instead of classes, which will be applied to
315 the template class. Again, to take our example:
316  
317 # in /views/layouts/main/bou
318 # empty file
319  
320 # in /views/layouts/main/page
321
322 # the import of tags really needs to be here
323 # instead than in the 'bou' file
324 use Template::Caribou::Tags::HTML ':all';
325  
326 html {
327 head { title { 'My App' } };
328 body {
329 show( 'inner' );
330 };
331 }
332  
333 # in /views/hullo/bou
334
335 use Template::Caribou::Tags::HTML ':all';
336  
337 has name => ( is => 'ro' );
338  
339 # in /views/howdie/inner
340 h1 { 'hullo ' . $self->name . '!' };
341  
342 =head1 CONFIGURATION
343  
344 =over
345  
346 =item namespace
347  
348 The namespace under which the Caribou classes are created.
349 defaults to C<Dancer::View>.
350  
351 =item auto_reload
352  
353 If set to C<true>, the Caribou object will verify if any of the
354 template files have changed before rendering and, if that's the case,
355 will self-update. Defaults to C<false>.
356  
357 =back
358  
359 =head1 AUTHOR
360  
361 Yanick Champoux <yanick@babyl.dyndns.org>
362  
363 =head1 COPYRIGHT AND LICENSE
364  
365 This software is copyright (c) 2013 by Yanick Champoux.
366  
367 This is free software; you can redistribute it and/or modify it under
368 the same terms as the Perl 5 programming language system itself.
369  
370 =cut
« Build ↑ Summary