blib/lib/Dancer/Template/Caribou.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 138 CC 9 SCAR 21.3
CC
9
Cov
0%
generate_view_class
20.0
render
20.0
generate_layout_class
12.0
CRAP 8.4
1 package Dancer::Template::Caribou;
2 BEGIN {
3 $Dancer::Template::Caribou::AUTHORITY = 'cpan:YANICK';
4 }
5 {
6 $Dancer::Template::Caribou::VERSION = '0.1.1';
7 }
8 #ABSTRACT: Template::Caribou wrapper for Dancer
9  
10 use strict;
11 use warnings;
12  
13 use Path::Tiny;
14 use Path::Iterator::Rule;
15 use Moose::Util qw/ with_traits find_meta /;
16 use FindBin;
17 use Dancer::Config qw/ setting /;
18  
19 use Moo;
20  
21 extends 'Dancer::Template::Abstract';
22  
23 sub _build_name { 'Dancer::Template::Caribou' };
24  
25 has 'default_tmpl_ext' => (
26 is => 'ro',
27 default => sub { 'bou' },
28 );
29  
30 has view_class => (
31 is => 'ro',
32 default => sub { { } },
33 );
34  
35 has layout_class => (
36 is => 'ro',
37 default => sub { { } },
38 );
39  
40 has namespace => (
41 is => 'ro',
42 lazy => 1,
43 default => sub {
44 $_[0]->config->{namespace} || 'Dancer::View';
45 },
46 );
47  
48 sub BUILD {
49 my $self = shift;
50  
51 my $views_dir = setting 'views';
52  
53 my @views =
54 Path::Iterator::Rule->new->skip_dirs('layouts')->file->name('bou')->all(
55 $views_dir );
56  
57 $self->generate_view_class( $_ ) for @views;
58  
59 my @layouts =
60 Path::Iterator::Rule->new->file->name('bou')->all(
61 path( $views_dir, 'layouts' ) );
62  
63 $self->generate_layout_class( $_ ) for @layouts;
64 }
65  
66 sub generate_layout_class {
67 my( $self, $bou ) = @_;
68  
69 my $bou_dir = path($bou)->parent;
70 my $segment = ''.path($bou)->relative( setting( 'views').'/layouts')->parent;
71  
72 ( my $name = $segment ) =~ s#/#::#;
73 $name = join '::', $self->namespace, $name;
74  
75 my $inner = path($bou)->slurp;
76  
77 eval qq{
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 warn $@ if $@;
98  
99 $self->layout_class->{$segment} = $name;
100  
101 }
102  
103 sub generate_view_class {
104 my( $self, $bou ) = @_;
105  
106 my $bou_dir = path($bou)->parent;
107 my $segment = ''.path($bou)->relative(setting('views'))->parent;
108  
109 ( my $name = $segment ) =~ s#/#::#;
110 $name = join '::', $self->namespace, $name;
111  
112 return if $self->layout_class->{$segment};
113  
114 my $inner = path($bou)->slurp;
115  
116 eval qq{
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 warn $@ if $@;
142  
143 $self->view_class->{$segment} = $name;
144  
145 }
146  
147 sub apply_layout {
148 return $_[1];
149 }
150  
151 sub layout {
152 return $_[3];
153 }
154  
155 sub render {
156 my( $self, $template, $tokens ) = @_;
157  
158 $template =~ s/\.bou$//;
159  
160 my $class = $self->view_class->{$template};
161  
162 unless ( $class ) {
163 my $c = $template;
164 $c =~ s#/#::#g;
165 $c = join '::', $self->namespace, $c;
166 die "template '$template' not found\n"
167 unless eval { $c->DOES('Template::Caribou') };
168 $class = $c;
169 }
170  
171 if ( my $lay = Dancer::App->current->setting('layout') ) {
172 my $role = $self->layout_class->{$lay}
173 or die "layout '$lay' not defined\n";
174  
175 $class = with_traits( $class, $role,
176 )
177 }
178  
179 my $x = $class->new( %$tokens)->render('page');
180 use utf8;utf8::decode($x);
181 return $x;
182 }
183  
184 sub view {
185 my( $self, $view ) = @_;
186 return $view;
187 }
188  
189 sub view_exists {
190 1;
191 }
192  
193  
194 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