line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Squatting::View; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
#use strict; |
4
|
|
|
|
|
|
|
#use warnings; |
5
|
|
|
|
|
|
|
#no warnings 'redefine'; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
#our $AUTOLOAD; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# constructor |
10
|
|
|
|
|
|
|
sub new { |
11
|
1
|
|
|
1
|
1
|
3
|
my $class = shift; |
12
|
1
|
|
|
|
|
3
|
my $name = shift; |
13
|
1
|
|
|
|
|
9
|
bless { name => $name, @_ } => $class; |
14
|
|
|
|
|
|
|
} |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
# name of view |
17
|
|
|
|
|
|
|
sub name : lvalue { |
18
|
0
|
|
|
0
|
1
|
0
|
$_[0]->{name}; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
# name of view |
22
|
|
|
|
|
|
|
sub headers : lvalue { |
23
|
0
|
|
|
0
|
1
|
0
|
$_[0]->{headers}; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
# $content = $view->_render($template, $vars) # render $template |
27
|
|
|
|
|
|
|
# $content = $view->_render($template, $vars, '_') # render generic template |
28
|
|
|
|
|
|
|
sub _render { |
29
|
4
|
|
|
4
|
|
9
|
my ($self, $template, $vars, $alt) = @_; |
30
|
4
|
|
|
|
|
10
|
$self->{template} = $template; |
31
|
4
|
100
|
66
|
|
|
31
|
if (exists $self->{layout} && ($template !~ /^_/)) { |
32
|
2
|
100
|
|
|
|
7
|
$template = $alt if defined $alt; |
33
|
2
|
|
|
|
|
11
|
$self->{layout}($self, $vars, $self->{$template}($self, $vars)); |
34
|
|
|
|
|
|
|
} else { |
35
|
2
|
100
|
|
|
|
8
|
$template = $alt if defined $alt; |
36
|
2
|
|
|
|
|
7
|
$self->{$template}($self, $vars); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# forward to _render() |
41
|
|
|
|
|
|
|
sub AUTOLOAD { |
42
|
4
|
|
|
4
|
|
4400
|
my ($self, $vars) = @_; |
43
|
4
|
|
|
|
|
9
|
my $template = $AUTOLOAD; |
44
|
4
|
|
|
|
|
26
|
$template =~ s/.*://; |
45
|
4
|
100
|
66
|
|
|
38
|
if (exists $self->{$template} && ref($self->{$template}) eq 'CODE') { |
|
|
50
|
|
|
|
|
|
46
|
2
|
|
|
|
|
31
|
$self->_render($template, $vars); |
47
|
|
|
|
|
|
|
} elsif (exists $self->{_}) { |
48
|
2
|
|
|
|
|
8
|
$self->_render($template, $vars, '_'); |
49
|
|
|
|
|
|
|
} else { |
50
|
0
|
|
|
|
|
|
die("$template cannot be rendered."); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
0
|
|
|
sub DESTROY { } |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
1; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 NAME |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Squatting::View - default view class for Squatting |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 SYNOPSIS |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
package App::Views; |
65
|
|
|
|
|
|
|
use Squatting ':views'; |
66
|
|
|
|
|
|
|
our @V = ( |
67
|
|
|
|
|
|
|
V( |
68
|
|
|
|
|
|
|
'example', |
69
|
|
|
|
|
|
|
layout => sub { |
70
|
|
|
|
|
|
|
my ($self, $v, $content) = @_; |
71
|
|
|
|
|
|
|
"(header $content footer)"; |
72
|
|
|
|
|
|
|
}, |
73
|
|
|
|
|
|
|
home => sub { |
74
|
|
|
|
|
|
|
my ($self, $v) = @_; |
75
|
|
|
|
|
|
|
"Hello, $v->{name}"; |
76
|
|
|
|
|
|
|
}, |
77
|
|
|
|
|
|
|
_ => sub { |
78
|
|
|
|
|
|
|
my ($self, $v) = @_; |
79
|
|
|
|
|
|
|
"You tried to render $self->{template} which was not defined."; |
80
|
|
|
|
|
|
|
}, |
81
|
|
|
|
|
|
|
arbitrary_data => [ { is => 'ok' }, 2 ], |
82
|
|
|
|
|
|
|
) |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 DESCRIPTION |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
In Squatting, views are objects that contain many templates. Templates are |
88
|
|
|
|
|
|
|
represented by coderefs that will be treated as methods of a view object. The |
89
|
|
|
|
|
|
|
job of a template is to take a hashref of variables and return a string. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Typically, the hashref of variables will be the same as what's in |
92
|
|
|
|
|
|
|
C<$controller-Ev>. This is important to note, because if you want a session |
93
|
|
|
|
|
|
|
variable in C<$controller-Estate> to affect the template, you have to put |
94
|
|
|
|
|
|
|
it in C<$controller-Ev>. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 API |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 General Methods |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head3 $view = Squatting::View->new($name, %methods) |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
The constructor takes a name and a hash of attributes and coderefs. |
103
|
|
|
|
|
|
|
Note that the name must be unique within the package the view is defined. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head3 $view->name |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
This returns the name of the view. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head3 $view->headers |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
This returns a hashref of the outgoing HTTP headers. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 Template Methods |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head3 $content = $view->$template($v) |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Any coderef that was given to the constructor may be called by name. Templates |
118
|
|
|
|
|
|
|
should be passed in a hashref (C<$v>) with variables for it to use to generate |
119
|
|
|
|
|
|
|
the final output. |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head3 $content = $view->layout($v, $content) |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
If you define a template named "layout", it'll be used to wrap the |
124
|
|
|
|
|
|
|
content of all templates whose name do not begin with "_". You can |
125
|
|
|
|
|
|
|
use this feature to provide standard headers and footers for your |
126
|
|
|
|
|
|
|
pages. |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head3 $content = $view->_($v) |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
If you define a template named "_", this will act as a catch-all |
131
|
|
|
|
|
|
|
that can be asked to render anything that wasn't explicitly defined. |
132
|
|
|
|
|
|
|
It's like our version of C. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
B: You can find out what they tried to render by inspecting |
135
|
|
|
|
|
|
|
C<$self-E{template}>. |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
This feature is useful when you're using a file-based templating system like |
138
|
|
|
|
|
|
|
Tenjin or Template Toolkit, and you don't want to write a template sub for |
139
|
|
|
|
|
|
|
every single template. Instead, you can make C<$self-E{template}> |
140
|
|
|
|
|
|
|
correspond to a file on disk. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head3 $view->{$template} = \&coderef |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
You are allowed to directly replace the template coderefs with your own. |
145
|
|
|
|
|
|
|
The most common reason you'd do this would be to replace an app's default |
146
|
|
|
|
|
|
|
layout with your own. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
$view->{layout} = sub { |
149
|
|
|
|
|
|
|
my ($self, $v, $content) = @_; |
150
|
|
|
|
|
|
|
# ... |
151
|
|
|
|
|
|
|
}; |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 SEE ALSO |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
L, |
156
|
|
|
|
|
|
|
L |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=cut |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# Local Variables: *** |
161
|
|
|
|
|
|
|
# mode: cperl *** |
162
|
|
|
|
|
|
|
# indent-tabs-mode: nil *** |
163
|
|
|
|
|
|
|
# cperl-close-paren-offset: -2 *** |
164
|
|
|
|
|
|
|
# cperl-continued-statement-offset: 2 *** |
165
|
|
|
|
|
|
|
# cperl-indent-level: 2 *** |
166
|
|
|
|
|
|
|
# cperl-indent-parens-as-block: t *** |
167
|
|
|
|
|
|
|
# cperl-tab-always-indent: nil *** |
168
|
|
|
|
|
|
|
# End: *** |
169
|
|
|
|
|
|
|
# vim:tabstop=8 softtabstop=2 shiftwidth=2 shiftround expandtab |