Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTContext.pm @ 352:675cd1829255
working on TTView: added control classes support
author | cin |
---|---|
date | Thu, 10 Oct 2013 19:51:19 +0400 |
parents | cfd7570c2af2 |
children | feeb3bc4a818 |
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 { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
113 $this->modules->{$file} = undef; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
114 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
115 } |
345 | 116 } |
117 | |
118 $this->throw(Template::Constants::ERROR_FILE, "$name: not found"); | |
119 } | |
120 | |
347 | 121 sub display { |
122 my $this = shift; | |
123 my $model = shift; | |
348 | 124 my ($template, $args); |
347 | 125 |
126 if (ref $_[0] eq 'HASH') { | |
127 $args = shift; | |
128 } else { | |
129 $template = shift; | |
130 $args = shift; | |
131 } | |
132 | |
348 | 133 my $prefix = $this->prefix; |
134 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
135 if (not(($args and delete $args->{_no_resolve}) or ref $model)) { |
348 | 136 $prefix = $prefix ? "${prefix}.${model}" : $model; |
137 $model = $this->resolve_model($model); | |
138 } else { | |
139 $prefix = ""; | |
140 } | |
141 | |
142 $template = $template ? $this->find_template($template) : $this->find_template_for($model); | |
143 | |
144 return $this->render( | |
145 $template, | |
146 hashApply( | |
147 { | |
148 prefix => $prefix, | |
149 model => $model, | |
150 }, | |
151 $args | |
152 ) | |
153 ); | |
154 } | |
155 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
156 sub display_model { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
157 my $this = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
158 my $model = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
159 my ($template, $args); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
160 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
161 if (ref $_[0] eq 'HASH') { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
162 $args = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
163 } else { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
164 $template = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
165 $args = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
166 } |
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 $args ||= {}; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
169 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
170 my $prefix = delete $args->{prefix} || $this->prefix; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
171 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
172 if (my $rel = delete $args->{rel}) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
173 $prefix = $prefix ? "${prefix}.${rel}" : $rel; |
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 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
176 $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
|
177 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
178 return $this->render( |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
179 $template, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
180 hashApply( |
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 prefix => $prefix, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
183 model => $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 $args |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
186 ) |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
187 ); |
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 |
348 | 190 sub invoke_environment { |
191 my ($this,$code,$env) = @_; | |
192 | |
193 $env ||= {}; | |
194 | |
195 my $out = eval { | |
196 $this->localise( | |
197 hashApply( | |
198 { | |
352 | 199 aliases => $this->aliases || {}, |
348 | 200 root => $this->root || $this, |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
201 modules => $this->modules || {}, |
348 | 202 cache => TypeKeyedCollection->new(), |
203 display => sub { | |
204 $this->display(@_); | |
205 }, | |
206 render => sub { | |
207 $this->render(@_); | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
208 }, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
209 display_model => sub { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
210 $this->display_model(@_); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
211 }, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
212 tt_cache => {} |
348 | 213 }, |
214 $env | |
215 ) | |
216 ); | |
217 | |
218 &$code($this); | |
219 }; | |
220 | |
221 my $e = $@; | |
222 $this->delocalise(); | |
223 | |
224 die $e if $e; | |
225 | |
226 return $out; | |
227 } | |
228 | |
229 sub render { | |
230 my ($this,$template,$args) = @_; | |
231 | |
232 $args ||= {}; | |
233 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
234 my $info = ref $template ? $template : $this->find_template($template); |
348 | 235 |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
236 if (ref($info) ne 'HASH') { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
237 carp "got an invalid template object: $info (" . ref($info) . ")"; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
238 $info = { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
239 template => $info, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
240 base => $this->base, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
241 initialized => 1 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
242 }; |
348 | 243 } |
244 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
245 return $this->invoke_environment( |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
246 sub { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
247 my $ctx = shift; |
352 | 248 |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
249 unless($info->{initialized}) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
250 if(my $init = $info->{template}->blocks->{INIT}) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
251 $info->{initialized} = 1; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
252 eval { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
253 $ctx->visit($info->{template}->blocks); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
254 $ctx->include($init); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
255 }; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
256 $ctx->leave(); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
257 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
258 } |
352 | 259 |
260 if (my $class = $info->{class}) { | |
261 $class->new($this,$info->{template})->Render($args); | |
262 } else { | |
263 return $ctx->include($info->{template},$args); | |
264 } | |
348 | 265 }, |
349 | 266 hashMerge( |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
267 $info->{labels} || {}, |
349 | 268 { |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
269 base => $info->{base}, |
349 | 270 parent => $this |
271 } | |
272 ) | |
348 | 273 ) |
274 } | |
275 | |
276 sub resolve_model { | |
277 my ($this,$prefix) = @_; | |
278 | |
279 die ArgException->new(prefix => "the prefix must be specified") | |
280 unless defined $prefix; | |
281 | |
282 #TODO handle DOM models | |
283 | |
284 my @comp = map { $_, 0 } grep length($_), split(/\.|\[(\d+)\]/, $prefix); | |
347 | 285 |
348 | 286 return $this->stash->get(['model',0,@comp]); |
287 } | |
288 | |
289 sub find_template_for { | |
290 my ($this,$model) = @_; | |
347 | 291 |
348 | 292 my $type = typeof($model); |
347 | 293 |
348 | 294 return $this->find_template('templates/plain') unless $type; |
295 | |
296 if (my $template = $this->cache->Get($type)) { | |
297 return $template; | |
298 } else { | |
299 | |
300 no strict 'refs'; | |
301 | |
302 my @isa = $type; | |
303 | |
304 while (@isa) { | |
305 my $sclass = shift @isa; | |
306 | |
307 (my $name = $sclass) =~ s/:+/_/g; | |
308 | |
309 $template = $this->find_template("templates/$name"); | |
310 | |
311 if ($template) { | |
312 $this->cache->Set($sclass,$template); | |
313 return $template; | |
314 } | |
315 | |
316 push @isa, @{"${sclass}::ISA"}; | |
317 } | |
318 | |
319 } | |
320 | |
321 return; | |
347 | 322 } |
323 | |
349 | 324 sub get_real_file { |
325 my ($this,$fname) = @_; | |
326 | |
327 my @path = split(/\/+/,$fname); | |
328 | |
329 foreach my $provider (@{$this->load_templates || []}) { | |
330 foreach my $dir (@{$provider->paths || []}) { | |
331 my $realName = File::Spec->catfile($dir,@path); | |
332 return $realName if -f $realName; | |
333 } | |
334 } | |
335 } | |
336 | |
337 sub load_labels { | |
338 my ($this,$fname) = @_; | |
339 | |
340 $fname = $this->get_real_file($fname); | |
341 | |
342 my %vars; | |
343 | |
344 my $flabels = "$fname.labels"; | |
345 | |
346 if (-f $flabels) { | |
347 | |
348 my %labels; | |
349 $labels{default} = IMPL::Resources::Strings::ParseStringsMap($flabels); | |
350 | |
351 while(my($label,$text) = each %{$labels{default}}) { | |
352 $vars{$label} = sub { | |
353 my ($params) = @_; | |
354 my $locale = Resources->currentLocale; | |
355 | |
356 unless ($labels{$locale}) { | |
357 $labels{$locale} = -f "$fname.$locale" ? | |
358 IMPL::Resources::Strings::ParseStringsMap("$fname.$locale") : | |
359 {}; | |
360 } | |
361 | |
362 return FormatMessage(($labels{$locale}{$label} || $text),$params); | |
363 } | |
364 } | |
365 } | |
366 | |
367 return \%vars; | |
368 } | |
369 | |
345 | 370 1; |
371 | |
372 __END__ | |
373 | |
374 =pod | |
375 | |
376 =head1 NAME | |
377 | |
378 C<IMPL::Web::View::TTContext> - доработанная версия контекста | |
379 | |
380 =head1 DESCRIPTION | |
381 | |
382 Расширяет функции C<Template::Context> | |
383 | |
384 =begin plantuml | |
385 | |
386 @startuml | |
387 | |
348 | 388 object RootContext { |
345 | 389 document |
346 | 390 globals |
345 | 391 } |
392 | |
393 object DocumentContext { | |
346 | 394 base |
395 extends | |
396 } | |
397 | |
398 object ControlContext { | |
399 base | |
400 extends | |
345 | 401 } |
402 | |
348 | 403 RootContext o-- DocumentContext |
404 RootContext o-- ControlContext | |
345 | 405 |
346 | 406 Document -- DocumentContext |
407 Control - ControlContext | |
345 | 408 |
348 | 409 Loader . RootContext: <<creates>> |
346 | 410 Loader . Document: <<creates>> |
411 Loader -up- Registry | |
345 | 412 |
413 @enduml | |
414 | |
415 =end plantuml | |
416 | |
417 =head1 MEMBERS | |
418 | |
419 =head2 C<[get,set]base> | |
420 | |
421 Префикс пути для поиска шаблонов | |
422 | |
423 =head2 C<template($name)> | |
424 | |
425 Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса. | |
426 | |
427 =head2 C<clone()> | |
428 | |
429 Создает копию контекста, при этом C<stash> локализуется, таким образом | |
430 клонированный контекст имеет собственное пространство имен, вложенное в | |
431 пространство родительского контекста. | |
432 | |
433 =cut |