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