Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTContext.pm @ 354:9330835535b9
fixed view double rendering
author | cin |
---|---|
date | Wed, 16 Oct 2013 17:28:40 +0400 |
parents | feeb3bc4a818 |
children | 8dfb9df07d02 |
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', |
17 Loader => 'IMPL::Code::Loader' | |
348 | 18 }, |
19 base => [ | |
343 | 20 'Template::Context' => '@_' |
348 | 21 ] |
343 | 22 }; |
23 | |
347 | 24 BEGIN { |
348 | 25 no strict 'refs'; |
347 | 26 foreach my $prop (qw( |
348 | 27 root |
347 | 28 base |
29 tt_ext | |
348 | 30 tt_cache |
347 | 31 parent |
32 prefix | |
348 | 33 cache |
34 includes | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
35 modules |
352 | 36 aliases |
354 | 37 resolver |
347 | 38 )) { |
39 my $t = $prop; | |
40 | |
348 | 41 *{__PACKAGE__ . '::' . $prop} = sub { |
347 | 42 my $this = shift; |
43 return @_ ? $this->stash->set($t, @_) : $this->stash->get($t); | |
44 } | |
45 } | |
46 } | |
47 | |
343 | 48 sub clone { |
49 my $this = shift; | |
347 | 50 my $params = shift; |
343 | 51 |
52 $this->localise(); | |
53 | |
54 my $args = { %{$this} }; | |
55 | |
56 $this->delocalise(); | |
57 | |
348 | 58 my $class = ref($this); |
343 | 59 |
60 delete $args->{CONFIG}; | |
61 | |
347 | 62 my $clone = $class->new($args); |
63 | |
64 $clone->stash->update($params) if $params; | |
65 | |
66 return $clone; | |
345 | 67 } |
68 | |
69 sub find_template { | |
348 | 70 my ($this,$name) = @_; |
71 | |
72 my $cache = $this->tt_cache; | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
73 |
348 | 74 $this->tt_cache($cache = {}) unless $cache; |
345 | 75 |
348 | 76 if(my $tpl = $cache->{$name}) { |
77 return $tpl; | |
78 } | |
79 | |
80 my @inc = ($this->base, @{$this->includes || []}); | |
81 | |
345 | 82 my $ext = $this->tt_ext || ""; |
348 | 83 |
84 my $file; | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
85 |
345 | 86 foreach my $dir (@inc) { |
348 | 87 $file = $dir ? "$dir/$name" : $name; |
88 | |
89 my $base = join('/',splice([split(/\/+/,$file)],0,-1)); | |
90 | |
91 $file = $ext ? "$file.$ext" : $file; | |
92 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
93 if (exists($this->modules->{$file})) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
94 my $info = $this->modules->{$file}; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
95 return $cache->{$name} = $info |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
96 if $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
97 } else { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
98 if( my $tt = eval { $this->template($file) } ) { |
352 | 99 my $class; |
100 if ($class = $tt->class) { | |
101 $class = $this->aliases->{$class} || $class; | |
102 Loader->safe->Require($class); | |
103 } | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
104 my $info = { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
105 base => $base, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
106 labels => $this->load_labels($file), |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
107 template => $tt, |
352 | 108 initialized => 0, |
109 class => $class | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
110 }; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
111 $this->modules->{$file} = $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
112 return $cache->{$name} = $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
113 } else { |
353 | 114 my $err = $@; |
115 | |
116 for(my $t = $err; is($t,'Template::Exception'); $t = $t->info ) { | |
117 die $err unless $t->type eq Template::Constants::ERROR_FILE; | |
118 } | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
119 $this->modules->{$file} = undef; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
120 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
121 } |
345 | 122 } |
123 | |
124 $this->throw(Template::Constants::ERROR_FILE, "$name: not found"); | |
125 } | |
126 | |
347 | 127 sub display { |
128 my $this = shift; | |
129 my $model = shift; | |
348 | 130 my ($template, $args); |
347 | 131 |
132 if (ref $_[0] eq 'HASH') { | |
133 $args = shift; | |
134 } else { | |
135 $template = shift; | |
136 $args = shift; | |
137 } | |
138 | |
348 | 139 my $prefix = $this->prefix; |
140 | |
354 | 141 my $info; |
142 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
143 if (not(($args and delete $args->{_no_resolve}) or ref $model)) { |
354 | 144 $info = $this->resolve_model($model,$args); |
348 | 145 } else { |
354 | 146 $info = { |
147 model => $model, | |
148 prefix => "" | |
149 }; | |
348 | 150 } |
151 | |
354 | 152 $template = $template ? $this->find_template($template) : $this->find_template_for($info->{model}); |
348 | 153 |
154 return $this->render( | |
155 $template, | |
156 hashApply( | |
354 | 157 $info, |
348 | 158 $args |
159 ) | |
160 ); | |
161 } | |
162 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
163 sub display_model { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
164 my $this = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
165 my $model = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
166 my ($template, $args); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
167 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
168 if (ref $_[0] eq 'HASH') { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
169 $args = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
170 } else { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
171 $template = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
172 $args = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
173 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
174 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
175 $args ||= {}; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
176 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
177 my $prefix = delete $args->{prefix} || $this->prefix; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
178 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
179 if (my $rel = delete $args->{rel}) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
180 $prefix = $prefix ? "${prefix}.${rel}" : $rel; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
181 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
182 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
183 $template = $template ? $this->find_template($template) : $this->find_template_for($model); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
184 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
185 return $this->render( |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
186 $template, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
187 hashApply( |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
188 { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
189 prefix => $prefix, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
190 model => $model, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
191 }, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
192 $args |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
193 ) |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
194 ); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
195 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
196 |
348 | 197 sub invoke_environment { |
198 my ($this,$code,$env) = @_; | |
199 | |
200 $env ||= {}; | |
201 | |
354 | 202 my $ctx = ($this->root || $this)->clone(); |
203 | |
348 | 204 my $out = eval { |
354 | 205 $ctx->localise( |
348 | 206 hashApply( |
207 { | |
352 | 208 aliases => $this->aliases || {}, |
354 | 209 root => $this->root || $ctx, |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
210 modules => $this->modules || {}, |
348 | 211 cache => TypeKeyedCollection->new(), |
212 display => sub { | |
354 | 213 $ctx->display(@_); |
348 | 214 }, |
215 render => sub { | |
354 | 216 $ctx->render(@_); |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
217 }, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
218 display_model => sub { |
354 | 219 $ctx->display_model(@_); |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
220 }, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
221 tt_cache => {} |
348 | 222 }, |
223 $env | |
224 ) | |
225 ); | |
226 | |
354 | 227 &$code($ctx); |
348 | 228 }; |
229 | |
230 my $e = $@; | |
354 | 231 $ctx->delocalise(); |
348 | 232 |
233 die $e if $e; | |
234 | |
235 return $out; | |
236 } | |
237 | |
238 sub render { | |
239 my ($this,$template,$args) = @_; | |
240 | |
241 $args ||= {}; | |
242 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
243 my $info = ref $template ? $template : $this->find_template($template); |
348 | 244 |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
245 if (ref($info) ne 'HASH') { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
246 carp "got an invalid template object: $info (" . ref($info) . ")"; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
247 $info = { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
248 template => $info, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
249 base => $this->base, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
250 initialized => 1 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
251 }; |
348 | 252 } |
253 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
254 return $this->invoke_environment( |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
255 sub { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
256 my $ctx = shift; |
352 | 257 |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
258 unless($info->{initialized}) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
259 if(my $init = $info->{template}->blocks->{INIT}) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
260 $info->{initialized} = 1; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
261 eval { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
262 $ctx->visit($info->{template}->blocks); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
263 $ctx->include($init); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
264 }; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
265 $ctx->leave(); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
266 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
267 } |
352 | 268 |
269 if (my $class = $info->{class}) { | |
270 $class->new($this,$info->{template})->Render($args); | |
271 } else { | |
272 return $ctx->include($info->{template},$args); | |
273 } | |
348 | 274 }, |
349 | 275 hashMerge( |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
276 $info->{labels} || {}, |
349 | 277 { |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
278 base => $info->{base}, |
349 | 279 parent => $this |
280 } | |
281 ) | |
348 | 282 ) |
283 } | |
284 | |
285 sub resolve_model { | |
354 | 286 my ($this,$prefix,$args) = @_; |
348 | 287 |
288 die ArgException->new(prefix => "the prefix must be specified") | |
289 unless defined $prefix; | |
290 | |
291 #TODO handle DOM models | |
292 | |
354 | 293 if (my $resolver = $this->resolver) { |
294 return $this->$resolver($prefix,$args); | |
295 } | |
296 | |
348 | 297 my @comp = map { $_, 0 } grep length($_), split(/\.|\[(\d+)\]/, $prefix); |
347 | 298 |
354 | 299 return { |
300 model => $this->stash->get(['model',0,@comp]), | |
301 prefix => $this->prefix ? $this->prefix . ".$prefix" : $prefix | |
302 }; | |
348 | 303 } |
304 | |
305 sub find_template_for { | |
306 my ($this,$model) = @_; | |
347 | 307 |
348 | 308 my $type = typeof($model); |
347 | 309 |
348 | 310 return $this->find_template('templates/plain') unless $type; |
311 | |
312 if (my $template = $this->cache->Get($type)) { | |
313 return $template; | |
314 } else { | |
315 | |
316 no strict 'refs'; | |
317 | |
318 my @isa = $type; | |
319 | |
320 while (@isa) { | |
321 my $sclass = shift @isa; | |
322 | |
323 (my $name = $sclass) =~ s/:+/_/g; | |
324 | |
325 $template = $this->find_template("templates/$name"); | |
326 | |
327 if ($template) { | |
328 $this->cache->Set($sclass,$template); | |
329 return $template; | |
330 } | |
331 | |
332 push @isa, @{"${sclass}::ISA"}; | |
333 } | |
334 | |
335 } | |
336 | |
337 return; | |
347 | 338 } |
339 | |
349 | 340 sub get_real_file { |
341 my ($this,$fname) = @_; | |
342 | |
343 my @path = split(/\/+/,$fname); | |
344 | |
345 foreach my $provider (@{$this->load_templates || []}) { | |
346 foreach my $dir (@{$provider->paths || []}) { | |
347 my $realName = File::Spec->catfile($dir,@path); | |
348 return $realName if -f $realName; | |
349 } | |
350 } | |
351 } | |
352 | |
353 sub load_labels { | |
354 my ($this,$fname) = @_; | |
355 | |
356 $fname = $this->get_real_file($fname); | |
357 | |
358 my %vars; | |
359 | |
360 my $flabels = "$fname.labels"; | |
361 | |
362 if (-f $flabels) { | |
363 | |
364 my %labels; | |
365 $labels{default} = IMPL::Resources::Strings::ParseStringsMap($flabels); | |
366 | |
367 while(my($label,$text) = each %{$labels{default}}) { | |
368 $vars{$label} = sub { | |
369 my ($params) = @_; | |
370 my $locale = Resources->currentLocale; | |
371 | |
372 unless ($labels{$locale}) { | |
373 $labels{$locale} = -f "$fname.$locale" ? | |
374 IMPL::Resources::Strings::ParseStringsMap("$fname.$locale") : | |
375 {}; | |
376 } | |
377 | |
378 return FormatMessage(($labels{$locale}{$label} || $text),$params); | |
379 } | |
380 } | |
381 } | |
382 | |
383 return \%vars; | |
384 } | |
385 | |
345 | 386 1; |
387 | |
388 __END__ | |
389 | |
390 =pod | |
391 | |
392 =head1 NAME | |
393 | |
394 C<IMPL::Web::View::TTContext> - доработанная версия контекста | |
395 | |
396 =head1 DESCRIPTION | |
397 | |
398 Расширяет функции C<Template::Context> | |
399 | |
400 =begin plantuml | |
401 | |
402 @startuml | |
403 | |
348 | 404 object RootContext { |
345 | 405 document |
346 | 406 globals |
345 | 407 } |
408 | |
409 object DocumentContext { | |
346 | 410 base |
411 extends | |
412 } | |
413 | |
414 object ControlContext { | |
415 base | |
416 extends | |
345 | 417 } |
418 | |
348 | 419 RootContext o-- DocumentContext |
420 RootContext o-- ControlContext | |
345 | 421 |
346 | 422 Document -- DocumentContext |
423 Control - ControlContext | |
345 | 424 |
348 | 425 Loader . RootContext: <<creates>> |
346 | 426 Loader . Document: <<creates>> |
427 Loader -up- Registry | |
345 | 428 |
429 @enduml | |
430 | |
431 =end plantuml | |
432 | |
433 =head1 MEMBERS | |
434 | |
435 =head2 C<[get,set]base> | |
436 | |
437 Префикс пути для поиска шаблонов | |
438 | |
439 =head2 C<template($name)> | |
440 | |
441 Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса. | |
442 | |
443 =head2 C<clone()> | |
444 | |
445 Создает копию контекста, при этом C<stash> локализуется, таким образом | |
446 клонированный контекст имеет собственное пространство имен, вложенное в | |
447 пространство родительского контекста. | |
448 | |
449 =cut |