Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTContext.pm @ 355:8dfb9df07d02
added support for the custom resolvers for the TTView
author | sergey |
---|---|
date | Thu, 17 Oct 2013 01:04:37 +0400 |
parents | 9330835535b9 |
children | 97131d500f16 |
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 |
347 | 37 )) { |
38 my $t = $prop; | |
39 | |
348 | 40 *{__PACKAGE__ . '::' . $prop} = sub { |
347 | 41 my $this = shift; |
42 return @_ ? $this->stash->set($t, @_) : $this->stash->get($t); | |
43 } | |
44 } | |
45 } | |
46 | |
343 | 47 sub clone { |
48 my $this = shift; | |
347 | 49 my $params = shift; |
343 | 50 |
51 $this->localise(); | |
52 | |
53 my $args = { %{$this} }; | |
54 | |
55 $this->delocalise(); | |
56 | |
348 | 57 my $class = ref($this); |
343 | 58 |
59 delete $args->{CONFIG}; | |
60 | |
347 | 61 my $clone = $class->new($args); |
62 | |
63 $clone->stash->update($params) if $params; | |
64 | |
65 return $clone; | |
345 | 66 } |
67 | |
68 sub find_template { | |
348 | 69 my ($this,$name) = @_; |
70 | |
71 my $cache = $this->tt_cache; | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
72 |
348 | 73 $this->tt_cache($cache = {}) unless $cache; |
345 | 74 |
348 | 75 if(my $tpl = $cache->{$name}) { |
76 return $tpl; | |
77 } | |
78 | |
79 my @inc = ($this->base, @{$this->includes || []}); | |
80 | |
345 | 81 my $ext = $this->tt_ext || ""; |
348 | 82 |
83 my $file; | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
84 |
345 | 85 foreach my $dir (@inc) { |
348 | 86 $file = $dir ? "$dir/$name" : $name; |
87 | |
88 my $base = join('/',splice([split(/\/+/,$file)],0,-1)); | |
89 | |
90 $file = $ext ? "$file.$ext" : $file; | |
91 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
92 if (exists($this->modules->{$file})) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
93 my $info = $this->modules->{$file}; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
94 return $cache->{$name} = $info |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
95 if $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
96 } else { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
97 if( my $tt = eval { $this->template($file) } ) { |
352 | 98 my $class; |
99 if ($class = $tt->class) { | |
100 $class = $this->aliases->{$class} || $class; | |
101 Loader->safe->Require($class); | |
102 } | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
103 my $info = { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
104 base => $base, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
105 labels => $this->load_labels($file), |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
106 template => $tt, |
352 | 107 initialized => 0, |
108 class => $class | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
109 }; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
110 $this->modules->{$file} = $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
111 return $cache->{$name} = $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
112 } else { |
353 | 113 my $err = $@; |
114 | |
115 for(my $t = $err; is($t,'Template::Exception'); $t = $t->info ) { | |
116 die $err unless $t->type eq Template::Constants::ERROR_FILE; | |
117 } | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
118 $this->modules->{$file} = undef; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
119 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
120 } |
345 | 121 } |
122 | |
123 $this->throw(Template::Constants::ERROR_FILE, "$name: not found"); | |
124 } | |
125 | |
347 | 126 sub display { |
127 my $this = shift; | |
128 my $model = shift; | |
348 | 129 my ($template, $args); |
347 | 130 |
131 if (ref $_[0] eq 'HASH') { | |
132 $args = shift; | |
133 } else { | |
134 $template = shift; | |
135 $args = shift; | |
136 } | |
137 | |
348 | 138 my $prefix = $this->prefix; |
139 | |
354 | 140 my $info; |
141 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
142 if (not(($args and delete $args->{_no_resolve}) or ref $model)) { |
354 | 143 $info = $this->resolve_model($model,$args); |
348 | 144 } else { |
354 | 145 $info = { |
146 model => $model, | |
147 prefix => "" | |
148 }; | |
348 | 149 } |
150 | |
355
8dfb9df07d02
added support for the custom resolvers for the TTView
sergey
parents:
354
diff
changeset
|
151 $template ||= $info->{template}; |
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 | |
355
8dfb9df07d02
added support for the custom resolvers for the TTView
sergey
parents:
354
diff
changeset
|
293 if (my $res = $this->stash->get(['resolver', [$this,$prefix,$args]] ) ) { |
8dfb9df07d02
added support for the custom resolvers for the TTView
sergey
parents:
354
diff
changeset
|
294 return $res; |
354 | 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 |