annotate Lib/IMPL/Web/Handler/View.pm @ 371:d5c8b955bf8d

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