Jifty/View/Declare/Helpers.pm untested

filter: Statementstmt 0.0% 0 / 124 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 245 CC 18 SCAR 24.3
CC
18
Cov
0%
_get_single
30.0
page
30.0
_page_class
20.0
CRAP 11.3
1 use warnings;
2 use strict;
3  
4 package Jifty::View::Declare::Helpers;
5 use Template::Declare::Tags;
6 use base qw/Template::Declare Exporter/;
7  
8 our @EXPORT = (
9 qw(hyperlink tangent redirect new_action
10 form_submit form_return form_next_page page content
11 wrapper request get set render_param render_hidden
12 render_action render_region render_mason
13 current_user js_handlers),
14 @Template::Declare::Tags::EXPORT,
15 @Template::Declare::Tags::TagSubs, # Backward compatibility only
16 @Template::Declare::Tags::TAG_SUB_LIST,
17 );
18  
19 =head1 NAME
20  
21 Jifty::View::Declare::Helpers - Additional subroutines for Jifty TD templates
22  
23 =head1 DESCRIPTION
24  
25 This library provides mixins to help build your application's user interface.
26  
27 =head1 METHODS
28  
29 =head2 Arguments
30  
31 =head3 get args
32  
33 Returns arguments as set in the dispatcher or with L</set> below.
34 If called in scalar context, pulls the first item in C<args> and returns it.
35 If called in list context, returns the values of all items in C<args>.
36  
37 my $action = get('action');
38 my ($action, $object) = get(qw(action object));
39  
40 =cut
41  
42 sub get {
43 if (wantarray) {
44 map { _get_single($_) } @_;
45 } else {
46 _get_single($_[0]);
47 }
48 }
49  
50 sub request; # defined later
51  
52 sub _get_single {
53 my $v = request->template_argument($_[0]);
54 $v = request->argument( $_[0] ) if not defined $v;
55 return $v if defined $v;
56  
57 if (request->top_request ne request() and $v = request->top_request->template_argument($_[0])) {
58 if (ref $v) {
59 warn("The template argument '$_[0]' was not explicitly passed to the current region ('@{[request->path]}'), and thus will not work if the region is ever refreshed. Unfortunately, it is a reference, so it can't be passed explicitly either. You'll need to explicitly pass some stringification of what it is to the region.".Carp::longmess);
60 } else {
61 warn("The template argument '$_[0]' was not explicitly passed to the the current region ('@{[request->path]}'), and thus will not work if the region is ever refreshed. Try passing it explicitly?");
62 }
63 }
64 return undef;
65 }
66  
67 =head3 set key => val [ key => val ...]
68  
69 Sets arguments for later grabbing with L<get>.
70  
71 =cut
72  
73 sub set {
74 while ( my ( $arg, $val ) = splice(@_, 0, 2) ) {
75 request->template_argument( $arg => $val );
76 }
77 }
78  
79 =head2 HTML pages and layouts
80  
81 =head3 page
82  
83 template 'foo' => page {{ title is 'Foo' } ... };
84  
85 or
86  
87 template 'foo' => page { title => 'Foo' } content { ... };
88  
89 Renders an HTML page wrapped in L</wrapper>, after calling
90 "/_elements/nav" and setting a content type. Generally, you shouldn't
91 be using "/_elements/nav" but a Dispatcher rule instead.
92  
93 If C<page/content> calling convention is used, the return value of the
94 first sub will be passed into wrapper as the second argument as a
95 hashref, as well as the last argument for the content sub.
96  
97 =cut
98  
99 sub page (&;$) {
100 unshift @_, undef if $#_ == 0;
101 my ( $meta, $code ) = @_;
102 my $ret = sub {
103 my $self = shift;
104 Jifty->web->response->content_type('text/html; charset=utf-8');
105 my $wrapper = Jifty->app_class('View')->can('wrapper') || \&wrapper;
106 my @metadata = $meta ? $meta->($self) : ();
107 my $metadata = $#metadata == 0 ? $metadata[0] : {@metadata};
108 local *is::title = sub { Carp::carp "Can't use 'title is' when mixing mason and TD" };
109 $wrapper->( sub { $code->( $self, $metadata ) }, $metadata );
110 };
111 $ret->() unless defined wantarray;
112 return $ret;
113 }
114  
115 =head3 content
116  
117 Helper function for page { ... } content { ... }, read L</page> instead.
118  
119 =cut
120  
121 sub content (&;$) {
122 # XXX: Check for only 1 arg
123 return $_[0];
124 }
125  
126 =head3 wrapper $coderef
127  
128 Render a page. $coderef is a L<Template::Declare> coderef.
129 This badly wants to be redone.
130  
131 =cut
132  
133 sub wrapper {
134 my $content_code = shift;
135 my $meta = shift;
136 my $page = _page_class( $meta )->new({ content_code => $content_code, _meta => $meta });
137  
138 my ($spa) = Jifty->find_plugin('Jifty::Plugin::SinglePage');
139  
140 # XXX: spa hooks should be moved to page handlers
141 if ($spa && $page->allow_single_page) {
142 # If it's a single page app, we want to either render a
143 # wrapper and then get the region or render just the content
144 if ( !Jifty->web->current_region ) {
145 $page->render_header;
146 $page->render_body(sub {
147 render_region( $spa->region_name,
148 path => Jifty->web->request->path );
149 });
150 $page->render_footer;
151 } else {
152 $page->done_header(1);
153 $page->render_page->();
154 }
155 }
156 else {
157 $page->render;
158 }
159 }
160  
161 sub _page_class {
162 my $hard_require = 0;
163 my $app_class = $_[0]->{'class'};
164 unless ( $app_class ) {
165 $app_class = get_current_attr('PageClass');;
166 delete $Template::Declare::Tags::ATTRIBUTES{ 'PageClass' };
167 }
168 $hard_require = 1 if $app_class;
169  
170 my $page_class = Jifty->app_class( $app_class || 'View::Page' );
171 $page_class = 'Jifty::View::Declare::Page'
172 unless Jifty::Util->_require( module => $page_class, quiet => !$hard_require );
173 # XXX: fallback, this is ugly
174 Jifty::Util->require( $page_class );
175 return $page_class;
176 }
177  
178 =head2 Forms and actions
179  
180 =head3 form CODE
181  
182 Takes a subroutine reference or block of perl as its only argument and renders
183 it as a Jifty C<form>, for example:
184  
185 my $action = new_action class => 'CreateTask';
186 form {
187 form_next_page url => '/';
188 render_action $action;
189 form_submit( label => _('Create') );
190 };
191  
192 You can specify the form name using L<Template::Declare::Tags/with>, like so:
193  
194 with (name => 'create_task'), form {
195 ...
196 };
197  
198 =cut
199  
200 {
201 no warnings qw/redefine/;
202 sub form (&;$) {
203 my $code = shift;
204  
205 smart_tag_wrapper {
206 outs_raw( Jifty->web->form->start(@_) );
207 $code->();
208 outs_raw( Jifty->web->form->end );
209 return '';
210 };
211 }
212 }
213  
214 =head3 new_action
215  
216 Shortcut for L<Jifty::Web/new_action>.
217  
218 =cut
219  
220 sub new_action(@) {
221 return Jifty->web->new_action(@_);
222 }
223  
224 =head3 render_action $action_object, $fields, $args_to_pass_to_action
225  
226 Renders an action out of whole cloth.
227  
228 Arguments
229  
230 =over 4
231  
232 =item $action_object
233  
234 A Jifty::Action object which has already been initialized
235  
236 =item $fields
237  
238 A reference to an array of fields that should be rendered when
239 displaying this action. If left undefined, all of the
240 action's fields will be rendered.
241  
242 =item $args_to_pass_to_action
243  
244 A hashref of arguments that should be passed to $action->form_field for
245 every field of this action.
246  
247 =back
248  
249 =cut
250  
251 sub render_action(@) {
252 my ( $action, $fields, $field_args ) = @_;
253
254 my @f = ($fields && ref ($fields) eq 'ARRAY') ? @$fields : $action->argument_names;
255 foreach my $argument (@f) {
256 outs_raw( $action->form_field( $argument, %$field_args ) );
257 }
258 }
259  
260 =head2 render_param $action, $param, %args
261  
262 Takes an action and one or more arguments to pass to
263 L<< Jifty::Action->form_field >>.
264  
265 =cut
266  
267 sub render_param {
268 my $action = shift;
269 outs_raw( $action->form_field(@_) );
270 return '';
271 }
272  
273 =head2 render_hidden $action $name $default @args
274  
275 Takes an action and one or more arguments to pass to L<< Jifty::Action->hidden >>
276  
277 =cut
278  
279 sub render_hidden {
280 my $action = shift;
281 outs_raw( $action->hidden(@_) );
282 return '';
283 }
284  
285 =head3 form_return
286  
287 Shortcut for L<Jifty::Web::Form/return>.
288  
289 =cut
290  
291 sub form_return(@) {
292 outs_raw( Jifty->web->form->return(@_) );
293 '';
294 }
295  
296 =head3 form_submit
297  
298 Shortcut for L<Jifty::Web::Form/submit>.
299  
300 =cut
301  
302 sub form_submit(@) {
303 outs_raw( Jifty->web->form->submit(@_) );
304 '';
305 }
306  
307 =head3 form_next_page
308  
309 Shortcut for L<Jifty::Web::Form/next_page>.
310  
311 =cut
312  
313 sub form_next_page(@) {
314 Jifty->web->form->next_page(@_);
315 }
316  
317 =head2 Other functions and shortcuts
318  
319 =head3 hyperlink
320  
321 Shortcut for L<Jifty::Web/link>.
322  
323 =cut
324  
325 sub hyperlink(@) {
326 _function_wrapper( link => @_);
327 }
328  
329 sub _function_wrapper {
330 my $function = shift;
331 Template::Declare->buffer->append( Jifty->web->$function(@_)->render || '' );
332 return '';
333 }
334  
335  
336 =head3 tangent
337  
338 Shortcut for L<Jifty::Web/tangent>.
339  
340 =cut
341  
342 sub tangent(@) {
343 _function_wrapper( tangent => @_);
344 }
345  
346 =head3 redirect
347  
348 Shortcut for L<Jifty::Web/redirect>.
349  
350 =cut
351  
352 sub redirect(@) {
353 Jifty->web->redirect(@_);
354 return '';
355 }
356  
357 =head3 render_region
358  
359 A shortcut for C<< Jifty::Web::PageRegion->new(@_)->render >> which does the
360 L<Template::Declare> magic necessary to not mix its output with your current
361 page's.
362  
363 =cut
364  
365 sub render_region(@) {
366 unshift @_, 'name' if @_ % 2;
367 my $args = {@_};
368 my $path = $args->{path} ||= '/__jifty/empty';
369 if ($Template::Declare::Tags::self && $path !~ m|^/|) {
370 $args->{path} = $Template::Declare::Tags::self->path_for($path);
371 }
372 local $Template::Declare::Tags::self = undef;
373 Jifty::Web::PageRegion->new(%$args)->render;
374 }
375  
376  
377 =head3 request
378  
379 Shortcut for L<Jifty::Web/request>.
380  
381 =cut
382  
383 sub request {
384 Jifty->web->request;
385 }
386  
387 =head3 current_user
388  
389 Shortcut for L<Jifty::Web/current_user>.
390  
391 =cut
392  
393  
394 sub current_user {
395 Jifty->web->current_user;
396 }
397  
398 =head3 js_handlers
399  
400 Allows you to put javascript handlers, a la
401 L<Jifty::Web::Form::Element>, onto arbitrary HTML elements:
402  
403 div {
404 js_handlers {
405 onclick => { path => "/some/region/path" }
406 }
407 }
408  
409 =cut
410  
411 sub js_handlers(&;@) {
412 my $code = shift;
413 my $element = Jifty::Web::Form::Element->new({$code->()});
414 my %js = $element->javascript_attrs;
415 Template::Declare::Tags::append_attr($_ => $js{$_}) for keys %js;
416 return @_;
417 }
418  
419 =head3 render_mason PATH, ARGS
420  
421 Renders the Mason template at C<PATH> (a string) with C<ARGS> (a hashref).
422  
423 =cut
424  
425 sub render_mason {
426 my ($template, $args) = @_;
427 my $mason = Jifty->handler->view('Jifty::View::Mason::Handler');
428 $mason->handle_comp($template, $args);
429 return '';
430 }
431  
432 1;