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