annotate Lib/IMPL/Web/Handler/TTView.pm @ 336:86336d451b82

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