Jifty/Plugin/ViewDeclarePage/Page.pm untested

filter: Statementstmt 0.0% 0 / 159 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 17 totaltotal 0.0% 0 / 231 CC 7 SCAR 14.0
CC
7
Cov
0%
instrument_content
12.0
init
6.0
render_title_inpage
6.0
CRAP 4.1
1 package Jifty::Plugin::ViewDeclarePage::Page;
2  
3 use strict;
4 use warnings;
5 use base qw/Class::Accessor::Fast/;
6 use Jifty::View::Declare -base;
7  
8 =head1 NAME
9  
10 Jifty::View::Declare::Page::NewStyle - new style page wrapper to simplify customization and reuse.
11  
12 =head1 DESCRIPTION
13  
14 This library is a replacement for L<Jifty::View::Declare::Page>.
15 That is in Jifty for a while and can not be replaced with something
16 completely different because of backwards compatibility.
17  
18 When you declare a L<Jifty::View::Declare> template that is a page, for example:
19  
20 template 'news' => page {
21 ...
22 };
23  
24 Page classes come into game. First of all App::View::Page is loaded
25 (it's not true and there is a way to define class for particular page,
26 but lets leave it alone as interface is not settled enough to be
27 discussed). If the app class doesn't exist then default
28 L<Jifty::View::Declare::Page> is used. Code block right after page
29 token is used to render core of the page wrapped into some layout.
30 Page classes implement such layouts.
31  
32 It's very hard to extended L<Jifty::View::Declare::Page> class as
33 it's written in such a way that forces you to copy&paste some
34 internals from the class to make overridden method work and don't
35 break things.
36  
37 I think this implementation is much better thing. To use this class
38 as a base for all your pages you can just add this plugin to your app
39 and simple YourApp::View::Page will be generated for you. However,
40 if you're here then you want to change layout, your App::View::Page
41 should be something like:
42  
43 package MyApp::View::Page;
44 use base qw(Jifty::Plugin::ViewDeclarePage::Page);
45 use Jifty::View::Declare::Helpers;
46  
47 ...
48  
49 1;
50  
51 =head1 DIFFERENCES FROM DEFAULT PAGE CLASS
52  
53 =over 4
54  
55 =item no calls into templates
56  
57 Yes, that's it. No call to show('/menu'), instead it's a method
58 L</render_navigation> here. Why? If it's subclassable then there is no
59 need to split functionality between different modules. You
60 can always return old behavior by using:
61  
62 sub render_navigation { show(/menu) }
63  
64 =item no render_pre_content_hook
65  
66 override L</render_content>.
67  
68 =item title is always rendered in page
69  
70 Even when there is no 'page_title is ...' in the content code,
71 see L</instrument_content> and L</render_title_inpage>.
72  
73 =item no html in title
74  
75 All HTML is just escaped. 99% of apps don't want to put tags inside
76 title. Sure, it's wrapped into <h1> tag. See L</render_title_inpage>
77  
78 =item new 'add rel ...' and 'add rev ...'
79  
80 Can be used in the content code to define feeds, relative links and other
81 cool stuff. See L</instrument_content> and L</render_link_inpage>.
82  
83 =item at last
84  
85 It's documented!
86  
87 =back
88  
89 =cut
90  
91  
92 __PACKAGE__->mk_accessors(qw(content_code _title _links _meta));
93 use constant allow_single_page => 1;
94  
95 =head1 ACCESSORS
96  
97 =over 4
98  
99 =item content_code
100  
101 Code reference that renders the core of the page, this is code block
102 right after page token in the following example:
103  
104 template 'news' => page {
105 ...
106 };
107  
108 Is set by jifty during construction of the page object.
109  
110 =item _meta
111  
112 A hash reference that is set by jifty during construction of
113 the page object. It's empty unless you use the following syntax:
114  
115 template my_page => page { some => 'value', ... } content {
116 ...
117 };
118  
119 In this case _meta is a reference to the hash that goes right after
120 page token and content_code is after content token.
121  
122 =item _title and _links
123  
124 These are internal temporary holders of corresponding data.
125  
126 =back
127  
128 =head1 METHODS
129  
130 =head2 Initialization and rendering driver
131  
132 =head3 new
133  
134 Sets up a new page class. Called by Jifty with content_code and
135 optional _meta.
136  
137 Calls L</init> right before returning new instance of the class.
138  
139 =cut
140  
141 sub new {
142 my $class = shift;
143 my $props = shift;
144 $props->{'_meta'} ||= {};
145 $props->{'_links'} ||= [];
146 my $self = $class->SUPER::new($props);
147 $self->init;
148 return $self;
149 }
150  
151 =head3 init
152  
153 Sets _title accessor from 'title' in _meta if the latter is defined,
154 so you can do the following if title of a page is static:
155  
156 template news => page { title => _('News') } content {
157 ...
158 };
159  
160 =cut
161  
162 sub init {
163 my $self = shift;
164  
165 if ( defined (my $title = $self->_meta->{'title'}) ) {
166 $self->_title( $title );
167 }
168  
169 return $self;
170 }
171  
172 =head3 render
173  
174 Renders whole page from doctype till closing html tag. Takes
175 no arguments.
176  
177 This method drives rendering of the page. Page is split
178 into three major parts: header, body and footer. Each is
179 implemented as corresponding method with 'render_' prefix.
180  
181 It worth to note that order of rendering is changed and
182 header is rendered after the body to allow you define page
183 title, RSS feeds and other things in content. Read more
184 about this below in L</render_header> and L</instrument_content>.
185  
186 =cut
187  
188 sub render {
189 my $self = shift;
190  
191 Template::Declare->buffer->push( private => 1 );
192 $self->render_body;
193 my $body = Template::Declare->buffer->pop;
194  
195 $self->render_header;
196  
197 Template::Declare->buffer->append( $body );
198  
199 $self->render_footer;
200 }
201  
202 =head2 Main blocks of the page
203  
204 =head3 render_header
205  
206 Renders an HTML "doctype" and complete <head> tag. Usually you don't
207 want to override this. This method is rendered after body and main
208 content of the page, so all things you need in head tag you can define
209 in content.
210  
211 =over 4
212  
213 =item doctype
214  
215 Calls L</render_doctype>.
216  
217 =item C<page_title is ...>
218  
219 You can define dynamic title using the following:
220  
221 template some => page {
222 my $page_title = ...;
223 ...
224 page_title is $page_title;
225 ...
226 };
227  
228 Don't want to define dynamic title then as well you can use syntax
229 described in L</init> above.
230  
231 When 'page_title is' is used in the content code, L</render_title_inpage>
232 is called, read more in L</instrument_content>.
233  
234 L</render_title_inhead> is called during rendering of the head tag,
235 so you can change title there as well by subclassing that method.
236  
237 =item tag <link>, C<add rel ...> and C<add rev ...>
238  
239 You can add <link> tags right from the content using the following syntax:
240  
241 add rel "alternate",
242 type => "application/atom+xml",
243 title => _('Updated this week'),
244 href => '/feeds/atom/recent',
245 ;
246  
247 When these constructions are used, L</render_link_inpage> is called
248 so you can add something right in the page content, for example
249 RSS image with link to the feed. See also L</instrument_content>.
250  
251 L</render_links_inhead> is called during rendering of the head tag.
252  
253 =item css and js
254  
255 Links to CSS and JS files are rendered for you using
256 L<Jifty::Web/include_css> and L<Jifty::Web/include_js> functions.
257  
258 Read docs around those methods and L<Jifty::Manual> on adding your
259 own styles and scripts.
260  
261 =item meta
262  
263 Not implemented, but will be as soon as syntax will be defined.
264  
265 =back
266  
267 =cut
268  
269 sub render_header {
270 my $self = shift;
271  
272 $self->render_doctype;
273  
274 head {
275 Jifty->web->response->content_type('text/html; charset=utf-8');
276 with(
277 'http-equiv' => "content-type",
278 content => "text/html; charset=utf-8"
279 ), meta {};
280 $self->render_title_inhead( $self->_title );
281 $self->render_links_inhead( @{ $self->_links || [] } );
282 Jifty->web->include_css;
283 Jifty->web->include_javascript;
284 };
285 return '';
286 };
287  
288 =head3 render_body
289  
290 Renders body tag, declares that we're in body and calls L</render_page>
291 that actually defines layout of the body. L</render_page> method is better
292 target for subclassing instead of this.
293  
294 =cut
295  
296 sub render_body {
297 my $self = shift;
298 body {
299 Jifty::View->call_trigger('body_start');
300 $self->render_page;
301 Jifty::View->call_trigger('body_end');
302 };
303 return '';
304 }
305  
306 =head3 render_footer
307  
308 Renders the page footer - </html> tag :)
309  
310 =cut
311  
312 sub render_footer {
313 my $self = shift;
314 outs_raw('</html>');
315 return '';
316 }
317  
318 =head2 Body layout
319  
320 =head3 render_page
321  
322 Renders the skeleton of the page and then calls L</instrument_content>
323 to prepare and finally render L</content_code> using L</render_content>.
324  
325 Default layout of the page is the following:
326  
327 <div>
328 <div>
329 this->render_salutation
330 this->render_navigation
331 </div>
332 <div id="content"><div>
333 this->instrument_content
334 this->render_jifty_page_detritus
335 </div></div>
336 </div>
337  
338 =cut
339  
340 sub render_page {
341 my $self = shift;
342 div {
343 div {
344 $self->render_salutation;
345 $self->render_navigation;
346 }
347 div { attr { id is 'content' };
348 div {
349 $self->instrument_content;
350 $self->render_jifty_page_detritus;
351 }
352 }
353 };
354 return '';
355 }
356  
357 =head3 instrument_content
358  
359 Something you don't want ever touch. However, does the following:
360  
361 =over 4
362  
363 =item setups local 'page_title is ...' handler which calls L</render_title_inpage>
364 if 'page_title is' is used.
365  
366 =item if 'page_title is' is not used then calls L</render_title_inpage> after
367 and put result into output stream before the content.
368  
369 =item setup handler for 'add rel ...' and 'add rev ...', that calls
370 L</render_link_inpage> and fills _links accessor.
371  
372 =item sure calls L</render_content>.
373  
374 =back
375  
376 =cut
377  
378 sub instrument_content {
379 my $self = shift;
380  
381 no warnings qw( redefine once );
382  
383 my $seen_title = 0;
384 local *is::page_title = sub {
385 shift;
386 $seen_title = 1;
387 no warnings qw(uninitialized);
388 my $res = '';
389 for (@_) {
390 # just in case somebody if somebody put a tag into title
391 # tags' code may play with buffers directly
392 if ( ref($_) eq 'CODE' ) {
393 Template::Declare->buffer->push( private => 1);
394 $_->();
395 $res .= Template::Declare->buffer->pop;
396 } else {
397 $res .= $_;
398 }
399 }
400 $self->_title( $self->_title . $res );
401 $self->render_title_inpage( $self->_title );
402 return '';
403 };
404  
405 local *rel::add = sub {
406 shift;
407 my %args = ('rel', @_);
408 my $links = $self->_links;
409 push @$links, \%args;
410 $self->_links( $links );
411 $self->render_link_inpage( %args );
412 return '';
413 };
414  
415 local *rev::add = sub {
416 shift;
417 my %args = ('rev', @_);
418 my $links = $self->_links;
419 push @$links, \%args;
420 $self->_links( $links );
421 $self->render_link_inpage( %args );
422 return '';
423 };
424  
425 Template::Declare->buffer->push( private => 1 );
426 $self->render_content;
427 my $content = Template::Declare->buffer->pop;
428  
429 unless ( $seen_title ) {
430 $self->render_title_inpage( $self->_title );
431 }
432  
433 Template::Declare->buffer->append( $content );
434 return '';
435 }
436  
437 =head3 render_content
438  
439 Renders content of the page - L</content_code>.
440  
441 =cut
442  
443 sub render_content {
444 my $self = shift;
445  
446 $self->content_code->();
447  
448 return '';
449 }
450  
451 =head2 Helpers
452  
453 =head3 render_doctype
454  
455 Renders default doctype (HTML5) and opening C<< <html> >> tag
456  
457 =cut
458  
459 sub render_doctype {
460 outs_raw("<!DOCTYPE html>\n<html>\n");
461 return '';
462 }
463  
464 =head3 render_title_inhead
465  
466 Should output nothing but a title tag what will be placed into the head.
467 Title is passed as only argument. Arguments are combined.
468  
469 =cut
470  
471 sub render_title_inhead {
472 my $self = shift;
473 my $title = shift
474 || Jifty->config->framework('ApplicationName');
475  
476 title { $title };
477 return '';
478 }
479  
480 =head3 render_title_inpage
481  
482 Renders the in-page title, followed by L<page navigation|Jifty::Web/page_navigation>
483 and L<jifty messages|Jifty::Web/render_messages>.
484  
485 See L</render_title_inhead>, L</instrument_content> and L</render_header>.
486  
487 =cut
488  
489 sub render_title_inpage {
490 my $self = shift;
491 my $title = shift;
492  
493 if ( $title ) {
494 h1 { attr { class => 'title' }; outs($title) };
495 }
496  
497 Jifty->web->page_navigation->render_as_menu;
498  
499 Jifty->web->render_messages;
500  
501 return '';
502 }
503  
504 =head3 render_links_inhead
505  
506 Renders link tags which are passed as list of hashes.
507  
508 =cut
509  
510 sub render_links_inhead {
511 my $self = shift;
512 my @links = @_;
513 foreach ( @links ) {
514 with ( %$_ ), link { };
515 }
516 return '';
517 }
518  
519 =head3 render_link_inpage
520  
521 Do nothing by default, but link as a hash is passed
522 when content has 'add rel ...' or 'add rev ...'.
523  
524 Read more in L</render_header> and L</instrument_content>.
525  
526 =cut
527  
528 sub render_link_inpage { return '' }
529  
530 =head3 render_navigation
531  
532 Renders L<Jifty::Web/navigation> as L<menu|Jifty::Web::Menu/render_as_menu>
533 wrapped into a div with id 'navigation'. There is as well page_navigation
534 in Jifty that is rendered in L</render_title_inpage> by default.
535  
536 Called from L</render_page>.
537  
538 =cut
539  
540 sub render_navigation {
541 my $self = shift;
542 my @args = @_;
543 div { attr { id => "navigation" };
544 Jifty->web->navigation->render_as_menu(@args);
545 };
546 return '';
547 }
548  
549 =head3 render_salutation
550  
551 Renders salutation for the current user wrapped into div with id equal to 'salutation'.
552 Called from L</render_page>.
553  
554 =cut
555  
556 sub render_salutation {
557 my $cu = Jifty->web->current_user;
558 div { attr {id => "salutation" };
559 if ( $cu->id and $cu->user_object ) {
560 _( 'Hiya, %1.', $cu->username );
561 }
562 else {
563 _("You're not currently signed in.");
564 }
565 };
566 return '';
567 }
568  
569 =head3 render_jifty_page_detritus
570  
571 Renders admin mode warning, the wait message, and the keybindings javascript.
572 Called from L</render_page>.
573  
574 =cut
575  
576 sub render_jifty_page_detritus {
577 if ( Jifty->admin_mode ) {
578 with( class => "warning admin_mode" ), div {
579 outs( _('Alert') . ': ' );
580 outs_raw(
581 Jifty->web->tangent(
582 label => _('Administration mode is enabled.'),
583 url => '/__jifty/admin/'
584 )
585 );
586 }
587 }
588  
589 with( id => "jifty-wait-message", style => "display: none" ),
590 div { _('Loading...') };
591  
592 div { id is "keybindings" };
593 }
594  
595 1;