annotate Lib/IMPL/Web/Handler/TTView.pm @ 343:9bdccdf1f50b

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