File Coverage

blib/lib/Dancer2/Plugin/TemplateFlute.pm
Criterion Covered Total %
statement 12 30 40.0
branch 0 16 0.0
condition 0 6 0.0
subroutine 4 6 66.6
pod 1 1 100.0
total 17 59 28.8


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::TemplateFlute;
2              
3 3     3   935428 use warnings;
  3         4  
  3         90  
4 3     3   10 use strict;
  3         3  
  3         46  
5              
6 3     3   1369 use Dancer2::Plugin;
  3         154433  
  3         20  
7 3     3   25969 use Dancer2::Plugin::TemplateFlute::Form;
  3         8  
  3         689  
8              
9             =head1 NAME
10              
11             Dancer2::Plugin::TemplateFlute - Dancer2 form handler for Template::Flute template engine
12              
13             =head1 VERSION
14              
15             Version 0.202
16              
17             =cut
18              
19             our $VERSION = '0.202';
20              
21             plugin_keywords 'form';
22              
23             sub form {
24 0     0 1   my $plugin = shift;
25              
26 0           my $name;
27 0 0         if ( @_ % 2 ) {
28 0           $name = shift;
29             }
30              
31 0           my %params = @_;
32 0 0         $params{name} = $name if $name;
33              
34 0           $plugin->app->log("debug", "form called with name: $params{name}");
35              
36 0           my $source = delete $params{source};
37              
38             # for POST default to body_parameters
39 0 0 0       $source = 'body' if ( !$source && $plugin->app->request->is_post );
40              
41 0 0         if ( $source ) {
42 0 0         if ( $source eq 'body' ) {
    0          
    0          
43 0           $params{values} = $plugin->app->request->body_parameters;
44             }
45             elsif ( $source eq 'query' ) {
46 0           $params{values} = $plugin->app->request->query_parameters;
47             }
48             elsif ( $source eq 'parameters' ) {
49 0           $params{values} = $plugin->app->request->parameters;
50             }
51             }
52              
53             my $form = Dancer2::Plugin::TemplateFlute::Form->new(
54 0     0     log_cb => sub { $plugin->app->logger_engine->log(@_) },
55 0           session => $plugin->app->session,
56             %params,
57             );
58              
59 0 0 0       $form->from_session if ( $source && $source eq 'session' );
60              
61 0           return $form;
62             }
63              
64             =head1 SYNOPSIS
65              
66             Display template with checkout form:
67            
68             get '/checkout' => sub {
69             my $form;
70              
71             $form = form( name => 'checkout', source => 'session' );
72            
73             template 'checkout', { form => $form };
74             };
75              
76             Retrieve form input from checkout form body:
77              
78             post '/checkout' => sub {
79             my ($form, $values);
80              
81             $form = form( name => 'checkout', source => 'body' );
82             $values = $form->values;
83             };
84              
85             Reset form after completion to prevent old data from
86             showing up on new form:
87              
88             form('checkout')->reset;
89              
90             If you have multiple forms then just pass the form token as an array reference:
91              
92             get '/cart' => sub {
93             my ( $cart_form, $inquiry_form );
94              
95             # 'source' defaults to 'session' if not provided
96             $cart_form = form( name => 'cart' );
97              
98             # equivalent to form( name => 'inquiry' )
99             $inquiry_form = form( 'inquiry' );
100            
101             template 'checkout', { form => [ $cart_form, $inquiry_form ] };
102             };
103              
104             =head1 KEYWORDS
105              
106             =head2 form <$name|%params>
107              
108             The following C<%params> are recognised:
109              
110             =head3 name
111              
112             The name of the form. Defaults to 'main'.
113              
114             =head3 values
115              
116             my $form = form( 'main', values => $params );
117              
118             The form parameters as a L object or something that can
119             be coerced into one.
120              
121             Instead of L you can also use L to set initial values.
122              
123             =head3 source
124              
125             my $form = form( name => 'main', source => 'body' );
126              
127             The following values are valid:
128              
129             =over
130              
131             =item body
132              
133             This sets the form values to the request body parameters
134             L.
135              
136             =item query
137              
138             This sets the form values to the request query parameters
139             L.
140              
141             =item parameters
142              
143             This sets the form values to the combined body and request query parameters
144             L.
145              
146             =item session
147              
148             Reads in values from the session. This is the default if no L or
149             L are specified.
150              
151             =back
152              
153             B if both L and L are supplied then L is
154             ignored.
155              
156             See L for details of other parameters
157             that can be used.
158              
159             =head1 DESCRIPTION
160            
161             C is used for forms with the
162             L templating engine.
163              
164             Form fields, values and errors are stored into and loaded from the session key
165             C
.
166              
167             =head1 AUTHORS
168              
169             Original Dancer plugin by:
170              
171             Stefan Hornburg (Racke), C<< >>
172              
173             Initial port to Dancer2 by:
174              
175             Evan Brown (evanernest), C<< evan at bottlenose-wine.com >>
176              
177             Rehacking to Dancer2's plugin2 and general rework:
178              
179             Peter Mottram (SysPete), C<< peter at sysnix.com >>
180              
181             =head1 BUGS
182              
183             Please report any bugs or feature requests via GitHub issues:
184             L.
185              
186             We will be notified, and then you'll automatically be notified of progress
187             on your bug as we make changes.
188              
189             =head1 ACKNOWLEDGEMENTS
190              
191              
192             =head1 LICENSE AND COPYRIGHT
193              
194             Copyright 2011-2016 Stefan Hornburg (Racke).
195              
196             Copyright 2015-1016 Evan Brown.
197              
198             Copyright 2016 Peter Mottram (SysPete).
199              
200             This program is free software; you can redistribute it and/or modify it
201             under the terms of either: the GNU General Public License as published
202             by the Free Software Foundation; or the Artistic License.
203              
204             See http://dev.perl.org/licenses/ for more information.
205              
206             =cut
207              
208             1;