Template/TemplateFlute.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Plugin/Form.pm ↑ Summary
1 package Dancer::Template::TemplateFlute;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Template::Flute;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Template::Flute::Iterator;
8 use Template::Flute::Utils;
9 use Template::Flute::I18N;
10 use Module::Load;
11 use Scalar::Util qw/blessed/;
12  
13 use Dancer::Config;
14  
15 use base 'Dancer::Template::Abstract';
16  
17 our $VERSION = '0.0141';
18  
19 =head1 NAME
20  
21 Dancer::Template::TemplateFlute - Template::Flute wrapper for Dancer
22  
23 =head1 VERSION
24  
25 Version 0.0141
26  
27 =head1 DESCRIPTION
28  
29 This class is an interface between Dancer's template engine abstraction layer
30 and the L<Template::Flute> module.
31  
32 In order to use this engine, use the template setting:
33  
34 template: template_flute
35  
36 The default template extension is ".html".
37  
38 =head2 LAYOUT
39  
40 Each layout needs a specification file and a template file. To embed
41 the content of your current view into the layout, put the following
42 into your specification file, e.g. F<views/layouts/main.xml>:
43  
44 <specification>
45 <value name="content" id="content" op="hook"/>
46 </specification>
47  
48 This replaces the contents of the following block in your HTML
49 template, e.g. F<views/layouts/main.html>:
50  
51 <div id="content">
52 Your content
53 </div>
54  
55 =head2 ITERATORS
56  
57 Iterators can be specified explicitly in the configuration file as below.
58  
59 engines:
60 template_flute:
61 iterators:
62 fruits:
63 class: JSON
64 file: fruits.json
65  
66 =head2 FILTER OPTIONS
67  
68 Filter options and classes can be specified in the configuration file as below.
69  
70 engines:
71 template_flute:
72 filters:
73 currency:
74 options:
75 int_curr_symbol: "$"
76 image:
77 class: "Flowers::Filters::Image"
78  
79 =head2 ADJUSTING URIS
80  
81 We automatically adjust links in the templates if the value of
82 C<request->path> is different from C<request->path_info>.
83  
84 =head2 EMBEDDING IMAGES IN EMAILS
85  
86 If you pass a value named C<email_cids>, which should be an empty hash
87 reference, all the images C<src> attributes will be rewritten using
88 the CIDs, and the reference will be populated with an hashref, as
89 documented in L<Template::Flute>
90  
91 Further options for the CIDs should be passed in an optional value
92 named C<cids>. See L<Template::Flute> for them.
93  
94  
95 =head2 DISABLE OBJECT AUTODETECTION
96  
97 Sometimes you want to pass values to a template which are objects, but
98 don't have an accessor, so they should be treated like hashrefs instead.
99  
100 By default, the class C<Dancer::Session::Abstract> is treated this way. You
101 can specify additional classes with the following syntax:
102  
103 engines:
104 template_flute:
105 autodetect:
106 disable:
107 - My::Class1
108 - My::Class2
109  
110  
111 The class matching is checked by L<Template::Flute> with C<isa>, so
112 any parent class would do.
113  
114 =head2 LOCALIZATION
115  
116 Templates can be localized using the Template::Flute::I18N module. You
117 can define a class that provides a method which takes as first (and
118 only argument) the string to translate, and returns the translated
119 one. You have to provide the class and the method. If the class is not
120 provided, no localization is done. If no method is specified,
121 'localize' will be used. The app will crash if the class doesn't
122 provide such method.
123  
124 B<Be sure to return the argument verbatim if the module is not able to
125 translate the string>.
126  
127 Example configuration, assuming the class C<MyApp::Lexicon> provides a
128 C<try_to_translate> method.
129  
130 engines:
131 template_flute:
132 i18n:
133 class: MyApp::Lexicon
134 method: try_to_translate
135  
136  
137 A class could be something like this:
138  
139 package MyTestApp::Lexicon;
140 use Dancer ':syntax';
141  
142 sub new {
143 my $class = shift;
144 debug "Loading up $class";
145 my $self = {
146 dictionary => {
147 en => {
148 'TRY' => 'Try',
149 },
150 it => {
151 'TRY' => 'Prova',
152 },
153 }
154 };
155 bless $self, $class;
156 }
157  
158 sub dictionary {
159 return shift->{dictionary};
160 }
161  
162 sub try_to_translate {
163 my ($self, $string) = @_;
164 my $lang = session('lang') || var('lang');
165 return $string unless $lang;
166 return $string unless $self->dictionary->{$lang};
167 my $tr = $self->dictionary->{$lang}->{$string};
168 defined $tr ? return $tr : return $string;
169 }
170  
171 1;
172  
173 Optionally, you can pass the options to instantiate the class in the
174 configuration. Like this:
175  
176 engines:
177 template_flute:
178 i18n:
179 class: MyApp::Lexicon
180 method: localize
181 options:
182 append: 'X'
183 prepend: 'Y'
184 lexicon: 'path/to/po/files'
185  
186 This will call
187  
188 MyApp::Lexicon->new(append => 'X', prepend => 'Y', lexicon => 'path/to/po/files');
189  
190 when the engine is initialized, and will call the C<localize> method
191 on it to get the translations.
192  
193 =head2 DEBUG TOOLS
194  
195 If you set C<check_dangling> in the engine stanza, the specification
196 will run a check (using the L<Template::Flute::Specification>'s
197 C<dangling> method) against the template to see if you have elements
198 of the specifications which are not bound to any HTML elements.
199  
200 In this case a debug message is issued (so keep in mind that with
201 higher logging level you are not going to see it).
202  
203 Example configuration:
204  
205 engines:
206 template_flute:
207 check_dangling: 1
208  
209 When the environment is set to C<development> this feature is turned
210 on by default. You can silence the logs by setting:
211  
212 engines:
213 template_flute:
214 disable_check_dangling: 1
215  
216 =head2 FORMS
217  
218 Dancer::Template::TemplateFlute includes a form plugin L<Dancer::Plugin::Form>,
219 which supports L<Template::Flute> forms.
220  
221 The token C<form> is reserved for forms. It can be a single
222 L<Dancer::Plugin::Form> object or an arrayref of
223 L<Dancer::Plugin::Form> objects.
224  
225 =head3 Typical usage for a single form.
226  
227 =head4 XML Specification
228  
229 <specification>
230 <form name="registration" link="name">
231 <field name="email"/>
232 <field name="password"/>
233 <field name="verify"/>
234 </form>
235 </specification>
236  
237 =head4 HTML
238  
239 <form class="frm-default" name="registration" action="/register" method="POST">
240 <fieldset>
241 <div class="reg-info">Info</div>
242 <ul>
243 <li>
244 <label>Email</label>
245 <input type="text" name="email"/>
246 </li>
247 <li>
248 <label>Password</label>
249 <input type="text" name="password"/>
250 </li>
251 <li>
252 <label>Confirm password</label>
253 <input type="text" name="verify" />
254 </li>
255 <li>
256 <input type="submit" value="Register" class="btn-submit" />
257 </li>
258 </ul>
259 </fieldset>
260 </form>
261  
262 =head4 Code
263  
264 any [qw/get post/] => '/register' => sub {
265 my $form = form('registration');
266 my %values = %{$form->values};
267 # VALIDATE, filter, etc. the values
268 $form->fill(\%values);
269 template register => {form => $form };
270 };
271  
272 =head3 Usage example for multiple forms
273  
274 =head4 Specification
275  
276 <specification>
277 <form name="registrationtest" link="name">
278 <field name="emailtest"/>
279 <field name="passwordtest"/>
280 <field name="verifytest"/>
281 </form>
282 <form name="logintest" link="name">
283 <field name="emailtest_2"/>
284 <field name="passwordtest_2"/>
285 </form>
286 </specification>
287  
288 =head4 HTML
289  
290 <h1>Register</h1>
291 <form class="frm-default" name="registrationtest" action="/multiple" method="POST">
292 <fieldset>
293 <div class="reg-info">Info</div>
294 <ul>
295 <li>
296 <label>Email</label>
297 <input type="text" name="emailtest"/>
298 </li>
299 <li>
300 <label>Password</label>
301 <input type="text" name="passwordtest"/>
302 </li>
303 <li>
304 <label>Confirm password</label>
305 <input type="text" name="verifytest" />
306 </li>
307 <li>
308 <input type="submit" name="register" value="Register" class="btn-submit" />
309 </li>
310 </ul>
311 </fieldset>
312 </form>
313 <h1>Login</h1>
314 <form class="frm-default" name="logintest" action="/multiple" method="POST">
315 <fieldset>
316 <div class="reg-info">Info</div>
317 <ul>
318 <li>
319 <label>Email</label>
320 <input type="text" name="emailtest_2"/>
321 </li>
322 <li>
323 <label>Password</label>
324 <input type="text" name="passwordtest_2"/>
325 </li>
326 <li>
327 <input type="submit" name="login" value="Login" class="btn-submit" />
328 </li>
329 </ul>
330 </fieldset>
331 </form>
332  
333  
334 =head4 Code
335  
336 any [qw/get post/] => '/multiple' => sub {
337 my $login = form('logintest');
338 debug to_dumper({params});
339 if (params->{login}) {
340 my %vals = %{$login->values};
341 # VALIDATE %vals here
342 $login->fill(\%vals);
343 }
344 else {
345 # pick from session
346 $login->fill;
347 }
348 my $registration = form('registrationtest');
349 if (params->{register}) {
350 my %vals = %{$registration->values};
351 # VALIDATE %vals here
352 $registration->fill(\%vals);
353 }
354 else {
355 # pick from session
356 $registration->fill;
357 }
358 template multiple => { form => [ $login, $registration ] };
359 };
360  
361 =head1 METHODS
362  
363 =head2 default_tmpl_ext
364  
365 Returns default template extension.
366  
367 =head2 render TEMPLATE TOKENS
368  
369 Renders template TEMPLATE with values from TOKENS.
370  
371 =cut
372  
373 sub default_tmpl_ext {
374 return 'html';
375 }
376  
377 sub _i18n_obj {
378 my $self = shift;
379 unless (exists $self->{_i18n_obj}) {
380 my $conf = $self->config;
381 my $localize;
382 if ($conf and exists $conf->{i18n} and exists $conf->{i18n}->{class}) {
383 my $class = $conf->{i18n}->{class};
384 load $class;
385 my %args;
386 if ($conf->{i18n}->{options}) {
387 # do a shallow copy and pass that
388 %args = %{ $conf->{i18n}->{options} };
389 }
390 my $obj = $class->new(%args);
391 my $method = $conf->{i18n}->{method} || 'localize';
392 # store the closure in the object to avoid loading it up each time
393 $localize = sub {
394 my $to_translate = shift;
395 return $obj->$method($to_translate);
396 };
397 }
398 # provide a common interface with Template::Flute::I18N
399 $self->{_i18n_obj} = Template::Flute::I18N->new($localize);
400 }
401 return $self->{_i18n_obj};
402 }
403  
404  
405 sub render ($$$) {
406 my ($self, $template, $tokens) = @_;
407 my (%args, $flute, $html, $name, $value, %parms, %template_iterators, %iterators, $class);
408  
409 %args = (template_file => $template,
410 scopes => 1,
411 auto_iterators => 1,
412 values => $tokens,
413 filters => $self->config->{filters},
414 autodetect => { disable => [qw/Dancer::Session::Abstract/] },
415 );
416  
417 # determine whether we need to pass an adjust URI to Template::Flute
418 if (my $request = $tokens->{request}) {
419 my $pos = index($request->path, $request->path_info);
420 if ($pos > 0) {
421 $args{uri} = substr($request->path, 0, $pos);
422 }
423 }
424  
425 if (my $i18n = $self->_i18n_obj) {
426 $args{i18n} = $i18n;
427 }
428  
429 if (my $email_cids = $tokens->{email_cids}) {
430 $args{email_cids} = $email_cids;
431 # use the 'cids' tokens only if email_cids is defined
432 if (my $cid_options = $tokens->{cids}) {
433 $args{cids} = { %$cid_options };
434 }
435 }
436  
437 if ($self->config->{autodetect} && $self->config->{autodetect}->{disable}) {
438 push @{$args{autodetect}{disable}},
439 @{$self->config->{autodetect}->{disable}};
440 }
441  
442 $flute = Template::Flute->new(%args);
443  
444 # process HTML template to determine iterators used by template
445 $flute->process_template();
446  
447 # instantiate iterators where object isn't yet available
448 if (%template_iterators = $flute->template()->iterators) {
449 my $selector;
450  
451 for my $name (keys %template_iterators) {
452 if ($value = $self->config->{iterators}->{$name}) {
453 %parms = %$value;
454
455 $class = "Template::Flute::Iterator::$parms{class}";
456  
457 if ($parms{file}) {
458 $parms{file} = Template::Flute::Utils::derive_filename($template,
459 $parms{file}, 1);
460 }
461  
462 if ($selector = delete $parms{selector}) {
463 if ($selector eq '*') {
464 $parms{selector} = '*';
465 }
466 elsif ($tokens->{$selector}) {
467 $parms{selector} = {$selector => $tokens->{$selector}};
468 }
469 }
470  
471 eval "require $class";
472 if ($@) {
473 die "Failed to load class $class for iterator $name: $@\n";
474 }
475  
476 eval {
477 $iterators{$name} = $class->new(%parms);
478 };
479
480 if ($@) {
481 die "Failed to instantiate class $class for iterator $name: $@\n";
482 }
483  
484 $flute->specification->set_iterator($name, $iterators{$name});
485 }
486 }
487 }
488  
489 # check for forms
490 if (my @forms = $flute->template->forms()) {
491 if ($tokens->{form}) {
492 $self->_tf_manage_forms($flute, $tokens, @forms);
493 }
494 else {
495 Dancer::Logger::debug('Missing form parameters for forms ' .
496 join(", ", sort map { $_->name } @forms));
497 }
498 }
499 elsif ($tokens->{form}) {
500 my $form_name = blessed($tokens->{form}) ? $tokens->{form}->name : $tokens->{form};
501  
502 Dancer::Logger::debug("Form $form_name passed, but no forms found in the template $template.");
503 }
504  
505 $html = $flute->process();
506  
507 if ($self->config->{check_dangling} or
508 ($tokens->{settings}->{environment} eq 'development' &&
509 !$self->config->{disable_check_dangling})) {
510  
511 if (my @warnings = $flute->specification->dangling) {
512 foreach my $warn (@warnings) {
513 Dancer::Logger::debug('Found dangling element '
514 . $warn->{type} . ' ' . $warn->{name}
515 . ' (' , $warn->{dump} , ')');
516 }
517 }
518 }
519 return $html;
520 }
521  
522 sub _tf_manage_forms {
523 my ($self, $flute, $tokens, @forms) = @_;
524  
525 # simple case: only one form passed and one in the flute
526 if (ref($tokens->{form}) ne 'ARRAY') {
527 my $form_name = $tokens->{form}->name;
528 if (@forms == 1) {
529 my $form = shift @forms;
530 if ($form_name eq 'main' or
531 $form_name eq $form->name) {
532 # Dancer::Logger::debug("Filling the template form with" . Dumper($tokens->{form}->values));
533 $self->_tf_fill_forms($flute, $tokens->{form}, $form, $tokens);
534 }
535 }
536 else {
537 my $found = 0;
538 foreach my $form (@forms) {
539 # Dancer::Logger::debug("Filling the template form with" . Dumper($tokens->{form}->values));
540 if ($form_name eq $form->name) {
541 $self->_tf_fill_forms($flute, $tokens->{form}, $form, $tokens);
542 $found++;
543 }
544 }
545 if ($found != 1) {
546 Dancer::Logger::error("Multiple form are not being managed correctly, found $found corresponding forms, but we expected just one!")
547 }
548 }
549 }
550 else {
551 foreach my $passed_form (@{$tokens->{form}}) {
552 foreach my $form (@forms) {
553 if ($passed_form->name eq $form->name) {
554 $self->_tf_fill_forms($flute, $passed_form, $form, $tokens);
555 }
556 }
557 }
558 }
559 }
560  
561  
562 sub _tf_fill_forms {
563 my ($self, $flute, $passed_form, $form, $tokens) = @_;
564 # arguments:
565 # $flute is the template object.
566  
567 # $passed_form is the Dancer::Plugin::Form object we got from the
568 # tokens, which is $tokens->{form} when we have just a single one.
569  
570 # $form is the form object we got from the template itself, with
571 # $flute->template->forms
572  
573 # $tokens is the hashref passed to the template. We need it for the
574 # iterators.
575  
576 my ($iter, $action);
577 for my $name ($form->iterators) {
578 if (ref($tokens->{$name}) eq 'ARRAY') {
579 $iter = Template::Flute::Iterator->new($tokens->{$name});
580 $flute->specification->set_iterator($name, $iter);
581 }
582 }
583 if ($action = $passed_form->action()) {
584 $form->set_action($action);
585 }
586 $passed_form->fields([map {$_->{name}} @{$form->fields()}]);
587 $form->fill($passed_form->fill());
588  
589 if (Dancer::Config::settings->{session}) {
590 $passed_form->to_session;
591 }
592 }
593  
594  
595 =head1 SEE ALSO
596  
597 L<Dancer>, L<Template::Flute>
598  
599 =head1 AUTHOR
600  
601 Stefan Hornburg (Racke), <racke@linuxia.de>
602  
603 =head1 BUGS
604  
605 Please report any bugs or feature requests to C<bug-template-flute at rt.cpan.org>, or through
606 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Template-Flute>.
607  
608 =head1 SUPPORT
609  
610 You can find documentation for this module with the perldoc command.
611  
612 perldoc Template::Flute
613  
614 You can also look for information at:
615  
616 =over 4
617  
618 =item * RT: CPAN's request tracker
619  
620 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Template-TemplateFlute>
621  
622 =item * AnnoCPAN: Annotated CPAN documentation
623  
624 L<http://annocpan.org/dist/Dancer-Template-TemplateFlute>
625  
626 =item * CPAN Ratings
627  
628 L<http://cpanratings.perl.org/d/Dancer-Template-TemplateFlute>
629  
630 =item * Search CPAN
631  
632 L<http://search.cpan.org/dist/Dancer-Template-TemplateFlute/>
633  
634 =back
635  
636 =head1 LICENSE AND COPYRIGHT
637  
638 Copyright 2011-2015 Stefan Hornburg (Racke) <racke@linuxia.de>.
639  
640 This program is free software; you can redistribute it and/or modify it
641 under the terms of either: the GNU General Public License as published
642 by the Free Software Foundation; or the Artistic License.
643  
644 See http://dev.perl.org/licenses/ for more information.
645  
646 =cut
647  
648 1;
« Plugin/Form.pm ↑ Summary