File Coverage

lib/Egg/View/Mason.pm
Criterion Covered Total %
statement 24 46 52.1
branch 0 6 0.0
condition 0 10 0.0
subroutine 8 12 66.6
pod n/a
total 32 74 43.2


line stmt bran cond sub pod time code
1             package Egg::View::Mason;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Mason.pm 337 2008-05-14 12:30:09Z lushe $
6             #
7 1     1   408 use strict;
  1         1  
  1         35  
8 1     1   6 use warnings;
  1         2  
  1         141  
9              
10             our $VERSION= '3.00';
11              
12             sub _setup {
13 0     0     my($class, $e)= @_;
14 0           my $c= "$e->{namespace}::View::Mason"->config;
15 0   0       $c->{comp_root} ||= [ 'main' => $e->config->{template_path}[0] ];
16 0   0       $c->{data_dir} ||= $e->config->{dir}{tmp};
17 0           $class->next::method($e);
18             }
19              
20             package Egg::View::Mason::handler;
21 1     1   4 use strict;
  1         2  
  1         21  
22 1     1   4 use warnings;
  1         2  
  1         33  
23 1     1   5 use Carp qw/ croak /;
  1         1  
  1         50  
24 1     1   5 use base qw/ Egg::View /;
  1         2  
  1         94  
25 1     1   937 use HTML::Mason;
  1         107491  
  1         16  
26 1     1   87 use Egg::View::Template::GlobalParam;
  1         2  
  1         23  
27              
28             sub new {
29 0     0     my $view= shift->SUPER::new(@_);
30 0           $view->params({ Egg::View::Template::GlobalParam::set($view->e) });
31 0           $view;
32             }
33             sub render {
34 0     0     my $view = shift;
35 0   0       my $tmpl = shift || return(undef);
36 0 0         $tmpl =~m{^[^/]} and $tmpl= "/$tmpl";
37 0 0         my $args = $_[0] ? ($_[1] ? {@_}: $_[0]): {};
    0          
38 0           my $class= $view->e->namespace. '::View::Mason';
39 0           my $body;
40 0           my $mason= HTML::Mason::Interp->new(
41 0           %{$class->config}, %$args,
42             out_method => \$body,
43             allow_globals => [qw/$e $s $p/],
44             );
45 0           $mason->set_global(@$_) for (
46             [ '$e' => $view->{e} ],
47             [ '$s' => $view->{e}->stash ],
48             [ '$p' => $view->params ],
49             );
50 0           $mason->exec($tmpl);
51 0           \$body;
52             }
53             sub output {
54 0     0     my $view= shift;
55 0   0       my $tmpl= shift || $view->template || croak q{ I want template. };
56 0           $view->e->response->body( $view->render($tmpl, @_) );
57             }
58              
59             1;
60              
61             __END__
62              
63             =head1 NAME
64              
65             Egg::View::Mason - View for HTML::Mason
66              
67             =head1 SYNOPSIS
68              
69             __PACKAGE__->egg_startup(
70             ...
71             .....
72            
73             VIEW=> [
74             [ 'Mason' => {
75             comp_root=> [
76             [ main => '/path/to/root' ],
77             [ private=> '/path/to/comp' ],
78             ],
79             data_dir=> '/path/to/temp',
80             ... other HTML::Mason option.
81             } ],
82             ],
83            
84             );
85            
86             # The VIEW object is acquired.
87             my $view= $e->view('Mason');
88            
89             # It outputs it specifying the template.
90             my $content= $view->render('hoge.tt', \%option);
91              
92             =head1 DESCRIPTION
93              
94             It is a view to use the template of HTML::Mason.
95              
96             Please add Mason to the setting of VIEW to make it use.
97              
98             VIEW => [
99             [ Mason => { ... HTML::Mason option. (HASH) } ],
100             ],
101              
102             The global variable that can be used is as follows from the template.
103              
104             $e ... Object of project.
105             $s ... $e->stash
106             $p ... $e->view('Mason')->params
107             $m ... Object of HTML::Mason.
108              
109             =head1 HANDLER METHODS
110              
111             L<Egg::View> has been succeeded to.
112              
113             =head2 new
114              
115             Constructor.
116              
117             L<Egg::View::Template::GlobalParam> is set up.
118              
119             my $view= $e->view('Mason');
120              
121             =head2 render ([TEMPLATE_STR], [OPTION])
122              
123             It is L<HTML::Mason> as for the template of TEMPLATE_STR.
124             The result of evaluating is returned by the SCALAR reference.
125              
126             OPTION is HTML::Mason?. It is an option to pass. OPTION overwrites a set value
127             of the configuration.
128              
129             my $body= $view->render( 'foo.tt',
130             ..........
131             ....
132             );
133              
134             =head2 output ([TEMPLATE], [OPTION])
135              
136             The result of the render method is set in $e-E<gt>response-E<gt>body.
137              
138             When TEMPLATE is omitted, it acquires it from $view-E<gt>template.
139              
140             OPTION is passed to the render method as it is.
141              
142             $view->output;
143              
144             =head1 SEE ALSO
145              
146             L<Egg::Release>,
147             L<Egg::View>,
148             L<Egg::View::Template::GlobalParam>,
149             L<HTML::Mason>,
150              
151             =head1 AUTHOR
152              
153             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>.
158              
159             This library is free software; you can redistribute it and/or modify
160             it under the same terms as Perl itself, either Perl version 5.8.6 or,
161             at your option, any later version of Perl 5 you may have available.
162              
163             =cut
164