Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTContext.pm @ 404:9ef75f2029be default
sync
author | cin |
---|---|
date | Fri, 28 Aug 2015 19:54:53 +0300 |
parents | 38cb0b80e88e |
children |
rev | line source |
---|---|
343 | 1 package IMPL::Web::View::TTContext; |
2 use strict; | |
3 use Template::Base; | |
348 | 4 use Carp qw(carp); |
349 | 5 use File::Spec(); |
6 use IMPL::Resources::Format qw(FormatMessage); | |
7 use IMPL::Resources::Strings(); | |
343 | 8 |
348 | 9 use IMPL::Exception(); |
349 | 10 use IMPL::lang qw(is typeof hashApply hashMerge); |
343 | 11 use IMPL::declare { |
348 | 12 require => { |
13 Document => '-Template::Document', | |
14 TypeKeyedCollection => 'IMPL::TypeKeyedCollection', | |
349 | 15 ArgException => '-IMPL::InvalidArgumentException', |
352 | 16 Resources => 'IMPL::Resources', |
371 | 17 Loader => 'IMPL::Code::Loader', |
18 MetadataBase => '-IMPL::Web::View::Metadata::BaseMeta', | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
19 Metadata => 'IMPL::Web::View::Metadata::ObjectMeta', |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
20 StringMap => 'IMPL::Resources::StringLocaleMap' |
348 | 21 }, |
22 base => [ | |
343 | 23 'Template::Context' => '@_' |
348 | 24 ] |
343 | 25 }; |
26 | |
347 | 27 BEGIN { |
348 | 28 no strict 'refs'; |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
29 # modules is a global (for the whole document) templates cache |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
30 # tt_cache is a local (for the current context only) templtes cache |
347 | 31 foreach my $prop (qw( |
348 | 32 root |
347 | 33 base |
34 tt_ext | |
348 | 35 tt_cache |
347 | 36 parent |
37 prefix | |
348 | 38 cache |
39 includes | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
40 modules |
352 | 41 aliases |
356 | 42 id |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
43 metadata |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
44 model |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
45 templateInfo |
347 | 46 )) { |
47 my $t = $prop; | |
48 | |
348 | 49 *{__PACKAGE__ . '::' . $prop} = sub { |
347 | 50 my $this = shift; |
51 return @_ ? $this->stash->set($t, @_) : $this->stash->get($t); | |
52 } | |
53 } | |
54 } | |
55 | |
343 | 56 sub clone { |
57 my $this = shift; | |
347 | 58 my $params = shift; |
343 | 59 |
60 $this->localise(); | |
61 | |
62 my $args = { %{$this} }; | |
63 | |
64 $this->delocalise(); | |
65 | |
348 | 66 my $class = ref($this); |
343 | 67 |
68 delete $args->{CONFIG}; | |
69 | |
347 | 70 my $clone = $class->new($args); |
71 | |
72 $clone->stash->update($params) if $params; | |
73 | |
74 return $clone; | |
345 | 75 } |
76 | |
356 | 77 sub get_next_id { |
78 my ($this) = @_; | |
79 | |
80 my $id = $this->stash->get('document.nextId') || 0; | |
81 $this->stash->set('document.nextId', $id + 1); | |
82 return "w-$id"; | |
83 } | |
84 | |
345 | 85 sub find_template { |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
86 my ($this,$name, $nothrow) = @_; |
348 | 87 |
88 my $cache = $this->tt_cache; | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
89 |
348 | 90 $this->tt_cache($cache = {}) unless $cache; |
345 | 91 |
348 | 92 if(my $tpl = $cache->{$name}) { |
93 return $tpl; | |
94 } | |
95 | |
96 my @inc = ($this->base, @{$this->includes || []}); | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
97 #my @inc = @{$this->includes || []}; |
348 | 98 |
345 | 99 my $ext = $this->tt_ext || ""; |
348 | 100 |
394 | 101 #warn "find: $name"; |
102 | |
348 | 103 my $file; |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
104 |
345 | 105 foreach my $dir (@inc) { |
348 | 106 $file = $dir ? "$dir/$name" : $name; |
107 | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
108 my @parts = split(/\/+/,$file); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
109 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
110 my $templateName = pop @parts; |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
111 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
112 my $base = join('/',@parts); |
348 | 113 |
114 $file = $ext ? "$file.$ext" : $file; | |
115 | |
394 | 116 #warn " file: $file"; |
117 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
118 if (exists($this->modules->{$file})) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
119 my $info = $this->modules->{$file}; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
120 return $cache->{$name} = $info |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
121 if $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
122 } else { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
123 if( my $tt = eval { $this->template($file) } ) { |
398 | 124 #warn " found: $file"; |
352 | 125 my $class; |
126 if ($class = $tt->class) { | |
127 $class = $this->aliases->{$class} || $class; | |
128 Loader->safe->Require($class); | |
129 } | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
130 my $info = { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
131 base => $base, |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
132 name => $templateName, |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
133 template => $tt, |
352 | 134 initialized => 0, |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
135 class => $class, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
136 file => $file |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
137 }; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
138 $this->modules->{$file} = $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
139 return $cache->{$name} = $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
140 } else { |
353 | 141 my $err = $@; |
142 | |
398 | 143 #warn " not found: $err"; |
144 | |
353 | 145 for(my $t = $err; is($t,'Template::Exception'); $t = $t->info ) { |
146 die $err unless $t->type eq Template::Constants::ERROR_FILE; | |
147 } | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
148 $this->modules->{$file} = undef; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
149 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
150 } |
345 | 151 } |
152 | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
153 $this->throw(Template::Constants::ERROR_FILE, "$name: not found") |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
154 unless $nothrow; |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
155 return; |
345 | 156 } |
157 | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
158 sub display_for { |
347 | 159 my $this = shift; |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
160 my $path = shift; |
348 | 161 my ($template, $args); |
347 | 162 |
163 if (ref $_[0] eq 'HASH') { | |
164 $args = shift; | |
165 } else { | |
166 $template = shift; | |
167 $args = shift; | |
168 } | |
169 | |
348 | 170 my $prefix = $this->prefix; |
171 | |
354 | 172 my $info; |
371 | 173 my $meta = $this->resolve_model($path,$args) |
174 or return "[not found '$path']"; | |
354 | 175 |
371 | 176 $info->{prefix} = join('.', grep($_, $prefix, $path)); |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
177 $info->{model} = $meta->model; |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
178 $info->{metadata} = $meta; |
348 | 179 |
355
8dfb9df07d02
added support for the custom resolvers for the TTView
sergey
parents:
354
diff
changeset
|
180 $template ||= $info->{template}; |
371 | 181 $template = $template ? $this->find_template($template) : $this->find_template_for($info->{metadata}); |
348 | 182 |
183 return $this->render( | |
184 $template, | |
185 hashApply( | |
354 | 186 $info, |
348 | 187 $args |
188 ) | |
189 ); | |
190 } | |
191 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
192 sub display_model { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
193 my $this = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
194 my $model = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
195 my ($template, $args); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
196 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
197 if (ref $_[0] eq 'HASH') { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
198 $args = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
199 } else { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
200 $template = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
201 $args = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
202 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
203 |
371 | 204 #copy |
205 $args = { %{$args || {}} }; | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
206 |
371 | 207 $args->{prefix} = join('.',grep($_,$this->prefix,$args->{path})) |
208 unless defined $args->{prefix}; | |
209 | |
210 if (is($model,MetadataBase)) { | |
211 $args->{model} = $model->model; | |
212 $args->{metadata} = $model; | |
213 } else { | |
214 $args->{model} = $model; | |
215 $args->{metadata} = Metadata->GetMetadataForModel($model); | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
216 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
217 |
371 | 218 $template = $template ? $this->find_template($template) : $this->find_template_for($args->{metadata}); |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
219 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
220 return $this->render( |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
221 $template, |
371 | 222 $args |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
223 ); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
224 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
225 |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
226 # обеспечивает необходимый уровень изоляции между контекстами |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
227 # $code - код, который нужно выполнить в новом контексте |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
228 # $env - хеш с переменными, которые будут переданы в новый контекст |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
229 # в процессе будет создан клон корневого контекста, со всеми его свойствами |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
230 # затем новый контекст будет локализован и в него будут добавлены новые переменные из $env |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
231 # созданный контекст будет передан параметром в $code |
348 | 232 sub invoke_environment { |
233 my ($this,$code,$env) = @_; | |
234 | |
235 $env ||= {}; | |
236 | |
354 | 237 my $ctx = ($this->root || $this)->clone(); |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
238 |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
239 my @includes = @{$this->includes || []}; |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
240 |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
241 if ($this->base) { |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
242 unshift @includes, $this->base; |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
243 } |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
244 |
348 | 245 my $out = eval { |
354 | 246 $ctx->localise( |
348 | 247 hashApply( |
248 { | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
249 includes => \@includes, |
352 | 250 aliases => $this->aliases || {}, |
354 | 251 root => $this->root || $ctx, |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
252 modules => $this->modules || {}, |
348 | 253 cache => TypeKeyedCollection->new(), |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
254 display_for => sub { |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
255 $ctx->display_for(@_); |
348 | 256 }, |
257 render => sub { | |
354 | 258 $ctx->render(@_); |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
259 }, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
260 display_model => sub { |
354 | 261 $ctx->display_model(@_); |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
262 }, |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
263 tt_cache => {}, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
264 labels => sub { |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
265 $ctx->load_labels(@_); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
266 } |
348 | 267 }, |
268 $env | |
269 ) | |
270 ); | |
271 | |
354 | 272 &$code($ctx); |
348 | 273 }; |
274 | |
275 my $e = $@; | |
354 | 276 $ctx->delocalise(); |
348 | 277 |
278 die $e if $e; | |
279 | |
280 return $out; | |
281 } | |
282 | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
283 # использует указанный шаблон для создания фрагмента документа |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
284 # шаблон может быть как именем, так и хешем, содержащим информацию |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
285 # о шаблоне. |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
286 # отдельно следует отметить, что данный метод создает новый контекст |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
287 # для выполнения шаблона в котором задает переменные base, parent, id |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
288 # а также создает переменные для строковых констант из labels |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
289 # хеш с переменными $args будет передан самому шаблону в момент выполнения |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
290 # если у шаблона указан класс элемента управления, то при выполнении шаблона |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
291 # будет создан экземпляр этого класса и процесс выполнения шаблона будет |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
292 # делегирован методу Render этого экземпляра. |
348 | 293 sub render { |
294 my ($this,$template,$args) = @_; | |
295 | |
296 $args ||= {}; | |
297 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
298 my $info = ref $template ? $template : $this->find_template($template); |
348 | 299 |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
300 if (ref($info) ne 'HASH') { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
301 carp "got an invalid template object: $info (" . ref($info) . ")"; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
302 $info = { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
303 template => $info, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
304 base => $this->base, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
305 initialized => 1 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
306 }; |
348 | 307 } |
394 | 308 |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
309 return $this->invoke_environment( |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
310 sub { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
311 my $ctx = shift; |
352 | 312 |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
313 unless($info->{initialized}) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
314 if(my $init = $info->{template}->blocks->{INIT}) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
315 $info->{initialized} = 1; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
316 eval { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
317 $ctx->visit($info->{template}->blocks); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
318 $ctx->include($init); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
319 }; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
320 $ctx->leave(); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
321 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
322 } |
352 | 323 |
324 if (my $class = $info->{class}) { | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
325 $class->new($ctx,$info->{template},$args)->Render({}); |
352 | 326 } else { |
327 return $ctx->include($info->{template},$args); | |
328 } | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
329 }, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
330 { |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
331 base => $info->{base}, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
332 parent => $this, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
333 id => $this->get_next_id, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
334 templateInfo => $info |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
335 } |
348 | 336 ) |
337 } | |
338 | |
339 sub resolve_model { | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
340 my ($this,$prefix) = @_; |
348 | 341 |
342 die ArgException->new(prefix => "the prefix must be specified") | |
343 unless defined $prefix; | |
344 | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
345 my $meta = $this->metadata; |
371 | 346 unless($meta) { |
347 $meta = Metadata->GetMetadataForModel($this->model); | |
348 $this->metadata($meta); | |
349 } | |
350 | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
351 foreach my $part (grep length($_), split(/\.|\[(\d+)\]/, $prefix)) { |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
352 last unless $meta; |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
353 if ($part =~ /^\d+$/) { |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
354 $meta = $meta->GetItem($part); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
355 } else { |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
356 $meta = $meta->GetProperty($part); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
357 } |
354 | 358 } |
359 | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
360 return $meta; |
348 | 361 } |
362 | |
363 sub find_template_for { | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
364 my ($this,$meta, $nothrow) = @_; |
347 | 365 |
371 | 366 die ArgException->new(meta => 'An invalid metadata is supplied') |
367 unless is($meta,MetadataBase); | |
368 | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
369 return $this->find_template($meta->template) |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
370 if ($meta->template); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
371 |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
372 my $type = $meta->modelType; |
347 | 373 |
348 | 374 return $this->find_template('templates/plain') unless $type; |
375 | |
376 if (my $template = $this->cache->Get($type)) { | |
377 return $template; | |
378 } else { | |
379 | |
380 no strict 'refs'; | |
381 | |
382 my @isa = $type; | |
383 | |
384 while (@isa) { | |
385 my $sclass = shift @isa; | |
386 | |
387 (my $name = $sclass) =~ s/:+/_/g; | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
388 my ($shortName) = ($sclass =~ m/(\w+)$/); |
348 | 389 |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
390 $template = $this->find_template("templates/$name",1) || $this->find_template("templates/$shortName",1); |
348 | 391 |
392 if ($template) { | |
393 $this->cache->Set($sclass,$template); | |
394 return $template; | |
395 } | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
396 |
381
ced5937ff21a
Custom getters/setters support method names in theirs definitions
cin
parents:
380
diff
changeset
|
397 #todo $meta->GetISA to implement custom hierachy |
348 | 398 push @isa, @{"${sclass}::ISA"}; |
399 } | |
400 | |
401 } | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
402 $this->throw(Template::Constants::ERROR_FILE, "can't find a template for the model $type") |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
403 unless $nothrow; |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
404 |
348 | 405 return; |
347 | 406 } |
407 | |
349 | 408 sub get_real_file { |
409 my ($this,$fname) = @_; | |
410 | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
411 return unless length $fname; |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
412 |
349 | 413 my @path = split(/\/+/,$fname); |
414 | |
415 foreach my $provider (@{$this->load_templates || []}) { | |
416 foreach my $dir (@{$provider->paths || []}) { | |
417 my $realName = File::Spec->catfile($dir,@path); | |
418 return $realName if -f $realName; | |
419 } | |
420 } | |
421 } | |
422 | |
423 sub load_labels { | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
424 my ($this,$data) = @_; |
349 | 425 |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
426 die ArgException->new("A hash reference is required") |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
427 unless ref($data) eq 'HASH'; |
349 | 428 |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
429 my $stringMap = StringMap->new($data); |
349 | 430 |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
431 $this->stash->update({ |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
432 map { |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
433 my $id = $_; |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
434 $id, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
435 sub { |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
436 $stringMap->GetString($id,@_); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
437 }; |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
438 } keys %$data |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
439 }); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
440 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
441 my $ti = $this->templateInfo || {}; |
349 | 442 |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
443 if (my $fullName = $this->get_real_file($ti->{file})) { |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
444 my ($vol,$dir,$fname) = File::Spec->splitpath($fullName); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
445 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
446 my $name = $this->templateInfo->{name}; |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
447 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
448 my $localePath = File::Spec->catpath($vol, File::Spec->catdir($dir,'locale'),''); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
449 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
450 $stringMap->name($name); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
451 $stringMap->paths($localePath); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
452 } |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
453 return; |
349 | 454 } |
455 | |
345 | 456 1; |
457 | |
458 __END__ | |
459 | |
460 =pod | |
461 | |
462 =head1 NAME | |
463 | |
464 C<IMPL::Web::View::TTContext> - доработанная версия контекста | |
465 | |
466 =head1 DESCRIPTION | |
467 | |
468 Расширяет функции C<Template::Context> | |
469 | |
470 =begin plantuml | |
471 | |
472 @startuml | |
473 | |
348 | 474 object RootContext { |
345 | 475 document |
346 | 476 globals |
345 | 477 } |
478 | |
479 object DocumentContext { | |
346 | 480 base |
481 extends | |
482 } | |
483 | |
484 object ControlContext { | |
485 base | |
486 extends | |
345 | 487 } |
488 | |
348 | 489 RootContext o-- DocumentContext |
490 RootContext o-- ControlContext | |
345 | 491 |
346 | 492 Document -- DocumentContext |
493 Control - ControlContext | |
345 | 494 |
348 | 495 Loader . RootContext: <<creates>> |
346 | 496 Loader . Document: <<creates>> |
497 Loader -up- Registry | |
345 | 498 |
499 @enduml | |
500 | |
501 =end plantuml | |
502 | |
503 =head1 MEMBERS | |
504 | |
505 =head2 C<[get,set]base> | |
506 | |
507 Префикс пути для поиска шаблонов | |
508 | |
509 =head2 C<template($name)> | |
510 | |
511 Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса. | |
512 | |
513 =head2 C<clone()> | |
514 | |
515 Создает копию контекста, при этом C<stash> локализуется, таким образом | |
516 клонированный контекст имеет собственное пространство имен, вложенное в | |
517 пространство родительского контекста. | |
518 | |
519 =cut |