annotate lib/IMPL/Web/Handler/View.pm @ 412:30e8c6a74937 ref20150831

working on di container (role based registrations)
author cin
date Mon, 21 Sep 2015 19:54:10 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Web::Handler::View;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4 use Carp qw(carp);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use List::Util qw(first);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use IMPL::lang;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 use IMPL::Const qw(:prop);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8 use IMPL::declare {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 require => {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 Factory => 'IMPL::Web::View::ObjectFactory',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 HttpResponse => 'IMPL::Web::HttpResponse',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12 Loader => 'IMPL::Code::Loader',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 ViewResult => 'IMPL::Web::ViewResult',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 Security => 'IMPL::Security'
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15 },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 base => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 'IMPL::Object' => undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 'IMPL::Object::Autofill' => '@_',
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 'IMPL::Object::Serializable' => undef
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 ],
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 props => [
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 contentType => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 contentCharset => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 view => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26 layout => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 selectors => PROP_RO,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 defaultDocument => PROP_RW,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 _selectorsCache => PROP_RW
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30 ]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 my ($this) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 $this->_selectorsCache([ map $this->ParseRule($_), @{$this->selectors || []} ]);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39 sub Invoke {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 my ( $this, $action, $next ) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 my $result = $next ? $next->($action) : undef;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 my $model;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 if( ref $result and eval { $result->isa(ViewResult) } ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 $model = $result->model;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 $model = $result;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 $result = ViewResult->new(model => $model);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 my $vars = {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 result => $result,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 request => sub { $action },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55 app => $action->application,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 location => $action->context->{resourceLocation},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 resource => $action->context->{resource},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58 layout => $this->layout,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 document => {},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 session => sub { Security->context },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61 user => sub { Security->principal },
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 security => sub { $action->security }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 my %responseParams = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66 type => $this->contentType,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 charset => $this->contentCharset,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 body => $this->view->display(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69 $model,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 $this->SelectView( $action, ref $model ),
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 $vars
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72 )
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 $responseParams{status} = $result->status if $result->status;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 $responseParams{cookies} = $result->cookies if ref $result->cookies eq 'HASH';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77 $responseParams{headers} = $result->headers if ref $result->headers eq 'HASH';
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 return HttpResponse->new(
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80 %responseParams
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 );
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 sub SelectView {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85 my ($this,$action) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 my @path;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 for(my $r = $action->context->{resource}; $r ; $r = $r->parent ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 unshift @path, {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 name => $r->id,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 class => typeof($r->model)
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 @path = map { name => $_}, split /\/+/, $action->query->path_info()
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97 unless (@path);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 return $this->MatchPath(\@path,$this->_selectorsCache) || $this->defaultDocument;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 sub ParseRule {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103 my ($this, $rule) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 my ($selector,$data) = split /\s+=>\s+/, $rule;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 my @parts;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 my $first = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109 my $weight = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 foreach my $part ( split /\//, $selector ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 # если первым символом является /
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112 # значит путь в селекторе абсолютный и не нужно
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 # добавлять "любой" элемент в начало
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 if($part) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 $weight ++;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 push @parts,{ any => 1 } if $first;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 push @parts,{ any => 1 } unless $first;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120 next;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123 my ($name,$class) = split /@/, $part;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
125 if ( my ( $varName, $rx ) = ( $name =~ m/^\{(?:(\w+)\:)?(.*)\}$/ ) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
126 #this is a regexp
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
127
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
128 push @parts, {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
129 rx => $rx,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
130 var => $varName,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
131 class => $class,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
132 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
133 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
134 push @parts, {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
135 name => length($name) ? $name : undef,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
136 class => $class,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
137 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
138 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
139 } continue {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
140 $first = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
141 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
142
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
143 return { selector => \@parts, data => $data, weight => $weight };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
144 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
145
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
146 sub MatchPath {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
147 my ($this,$path,$rules) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
148
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
149 $path ||= [];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
150 $rules ||= [];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
151
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
152 my @next;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
153
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
154 foreach my $segment (@$path) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
155 foreach my $rule (@$rules) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
156 my @selector = @{$rule->{selector}};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
157
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
158 my $part = shift @selector;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
159
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
160 # if this rule doesn't have a selector
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
161 next unless $part;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
162
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
163 if ($part->{any}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
164 #keep the rule for the next try
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
165 push @next, $rule;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
166
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
167 $part = shift @selector while $part->{any};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
168 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
169
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
170 my $newRule = {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
171 selector => \@selector,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
172 data => $rule->{data},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
173 weight => $rule->{weight},
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
174 vars => { %{$rule->{vars} || {}} }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
175 };
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
176
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
177 my $success = 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
178 if (my $class = $part->{class}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
179 $success = isclass($segment->{class},$class);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
180 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
181
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
182 if($success && (my $name = $part->{name})) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
183 $success = $segment->{name} eq $name;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
184 } elsif ($success && (my $rx = $part->{rx})) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
185 if( my @captures = ($segment->{name} =~ m/($rx)/) ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
186 $newRule->{vars}->{$part->{var}} = \@captures
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
187 if $part->{var};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
188 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
189 $success = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
190 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
191 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
192
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
193 push @next, $newRule if $success;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
194
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
195 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
196 $rules = [@next];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
197 undef @next;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
198 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
199
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
200 my $result = (
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
201 sort {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
202 $b->{weight} <=> $a->{weight}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
203 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
204 grep {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
205 scalar(@{$_->{selector}}) == 0
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
206 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
207 @$rules
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
208 )[0];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
209
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
210 if($result) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
211 my $data = $result->{data};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
212 $data =~ s/{(\w+)(?:\:(\d+))?}/
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
213 my ($name,$index) = ($1,$2 || 0);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
214
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
215 if ($result->{vars}{$name}) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
216 $result->{vars}{$name}[$index];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
217 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
218 "";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
219 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
220 /gex;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
221
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
222 return $data;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
223 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
224 return;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
225 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
226 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
227
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
228 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
229
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
230 __END__
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
231
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
232 =pod
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
233
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
234 =head1 NAME
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
235
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
236 C<IMPL::Web::Handler::TTView> - использует шаблоны для построения представления.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
237
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
238 =head1 SYNOPSIS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
239
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
240 =begin code xml
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
241
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
242 <item id="html-view" type="IMPL::Web::Handler::View">
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
243 <contentType>text/html</contentType>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
244 <view id="tt-loader" type="IMPL::Web::View::TTView">
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
245 <options type="HASH">
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
246 <INCLUDE_PATH type="IMPL::Config::Reference">
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
247 <target>IMPL::Config</target>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
248 <AppBase>view</AppBase>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
249 </INCLUDE_PATH>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
250 <INTERPOLATE>1</INTERPOLATE>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
251 <POST_CHOMP>1</POST_CHOMP>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
252 <ENCODING>utf-8</ENCODING>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
253 </options>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
254 <ext>.tt</ext>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
255 <initializer>global.tt</initializer>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
256 <layoutBase>layouts</layoutBase>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
257 </view>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
258 <defaultDocument>default</defaultDocument>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
259 <selectors type="ARRAY">
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
260 <item>@HASH => dump</item>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
261 <item>@My::Data::Product => product/info</item>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
262 <item>{action:.*} @My::Data::Product => product/{action}</item>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
263 </selectors>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
264 </item>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
265
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
266 =end code xml
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
267
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
268 =head1 DESCRIPTION
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
269
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
270 Подбирает шаблон для представления результата, полученного при выполнении следующего обработчика. При
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
271 выборе используется принцип похожий на селекторы C<CSS>, основывающийся на именах ресурсов и их типах
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
272 данных.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
273
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
274 Данный обработчик понимает определенные свойства контекста:
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
275
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
276 =over
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
277
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
278 =item * C<resourceLocation>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
279
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
280 В данном свойстве может быть передана информация о текущем расположении ресурса,
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
281 для которого строится представление. Эта информация будет доступна в шаблоне
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
282 через свойство документа C<location>.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
283
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
284 =item * C<environment>
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
285
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
286 В данном совойстве контекста передается дополнительная информация об окружении
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
287 ресурса, например, которую задали родительские ресурсы. Использование данного
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
288 свойства позволяет не загромождать ресурс реализацией функциональности по
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
289 поддержке окружения. Это свойство может быть ссылкой на функцию, что позволяет
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
290 формировать контекст только по необходимости, при этом указанная функция будет
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
291 выполнена только один раз, при первом обращении.
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
292
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
293 =back
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
294
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
295 =head1 SELECTORS
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
296
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
297 =begin text
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
298
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
299 syntax::= selector => template
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
300
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
301 selector::= ([>]segment-template[@class-name])
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
302
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
303 segment-template::= {'{'name:regular-expr'}'|segment-name}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
304
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
305 name::= \w+
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
306
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
307 segment-name::= \S+
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
308
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
309 class-name::= name[(::name)]
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
310
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
311 url-template@class => template
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
312
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
313 shoes => product/list
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
314 /shop//{action:*.}@My::Data::Product => product/{action}
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
315
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
316 stuff >list => product/list
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
317 details => product/details
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
318
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
319 =end text
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
320
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
321
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
322 =cut
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
323