Mercurial > pub > Impl
comparison Lib/IMPL/Web/View/TTContext.pm @ 370:cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
author | sergey |
---|---|
date | Tue, 10 Dec 2013 03:02:01 +0400 |
parents | 833e663796c4 |
children | d5c8b955bf8d |
comparison
equal
deleted
inserted
replaced
369:7c784144d2f1 | 370:cbf4febf0930 |
---|---|
23 | 23 |
24 BEGIN { | 24 BEGIN { |
25 no strict 'refs'; | 25 no strict 'refs'; |
26 # modules is a global (for the whole document) templates cache | 26 # modules is a global (for the whole document) templates cache |
27 # tt_cache is a local (for the current context only) templtes cache | 27 # tt_cache is a local (for the current context only) templtes cache |
28 # view is a special variable, which will be cloned and passed to the nested context | |
29 foreach my $prop (qw( | 28 foreach my $prop (qw( |
30 root | 29 root |
31 base | 30 base |
32 tt_ext | 31 tt_ext |
33 tt_cache | 32 tt_cache |
35 prefix | 34 prefix |
36 cache | 35 cache |
37 includes | 36 includes |
38 modules | 37 modules |
39 aliases | 38 aliases |
39 | |
40 id | 40 id |
41 view | 41 metadata |
42 model | |
42 )) { | 43 )) { |
43 my $t = $prop; | 44 my $t = $prop; |
44 | 45 |
45 *{__PACKAGE__ . '::' . $prop} = sub { | 46 *{__PACKAGE__ . '::' . $prop} = sub { |
46 my $this = shift; | 47 my $this = shift; |
138 return; | 139 return; |
139 } | 140 } |
140 | 141 |
141 sub display_for { | 142 sub display_for { |
142 my $this = shift; | 143 my $this = shift; |
143 my $model = shift; | 144 my $path = shift; |
144 my ($template, $args); | 145 my ($template, $args); |
145 | 146 |
146 if (ref $_[0] eq 'HASH') { | 147 if (ref $_[0] eq 'HASH') { |
147 $args = shift; | 148 $args = shift; |
148 } else { | 149 } else { |
151 } | 152 } |
152 | 153 |
153 my $prefix = $this->prefix; | 154 my $prefix = $this->prefix; |
154 | 155 |
155 my $info; | 156 my $info; |
156 | 157 my $meta = $this->resolve_model($path,$args); |
157 if (not(($args and delete $args->{_no_resolve}) or ref $model)) { | 158 |
158 $info = $this->resolve_model($model,$args); | 159 $info->{prefix} = $prefix ? $prefix . '.' . $path : $path; |
159 } else { | 160 $info->{model} = $meta->model; |
160 $info = { | 161 $info->{metadata} = $meta; |
161 model => $model, | |
162 prefix => "" | |
163 }; | |
164 } | |
165 | 162 |
166 $template ||= $info->{template}; | 163 $template ||= $info->{template}; |
167 $template = $template ? $this->find_template($template) : $this->find_template_for($info->{model}); | 164 $template = $template ? $this->find_template($template) : $this->find_template_for($info->{model}); |
168 | 165 |
169 return $this->render( | 166 return $this->render( |
225 my @includes = @{$this->includes || []}; | 222 my @includes = @{$this->includes || []}; |
226 | 223 |
227 if ($this->base) { | 224 if ($this->base) { |
228 unshift @includes, $this->base; | 225 unshift @includes, $this->base; |
229 } | 226 } |
230 | |
231 my $view = $this->view; | |
232 $view = ref $view eq 'HASH' ? { %{$view} } : {}; | |
233 | |
234 hashApply($view, delete $env->{view}); | |
235 | 227 |
236 my $out = eval { | 228 my $out = eval { |
237 $ctx->localise( | 229 $ctx->localise( |
238 hashApply( | 230 hashApply( |
239 { | 231 { |
249 $ctx->render(@_); | 241 $ctx->render(@_); |
250 }, | 242 }, |
251 display_model => sub { | 243 display_model => sub { |
252 $ctx->display_model(@_); | 244 $ctx->display_model(@_); |
253 }, | 245 }, |
254 tt_cache => {}, | 246 tt_cache => {} |
255 view => $view | |
256 }, | 247 }, |
257 $env | 248 $env |
258 ) | 249 ) |
259 ); | 250 ); |
260 | 251 |
326 ) | 317 ) |
327 ) | 318 ) |
328 } | 319 } |
329 | 320 |
330 sub resolve_model { | 321 sub resolve_model { |
331 my ($this,$prefix,$args) = @_; | 322 my ($this,$prefix) = @_; |
332 | 323 |
333 die ArgException->new(prefix => "the prefix must be specified") | 324 die ArgException->new(prefix => "the prefix must be specified") |
334 unless defined $prefix; | 325 unless defined $prefix; |
335 | 326 |
336 | 327 my $meta = $this->metadata; |
337 | 328 foreach my $part (grep length($_), split(/\.|\[(\d+)\]/, $prefix)) { |
338 if (my $res = $this->stash->get(['resolve', [$this,$prefix,$args]] ) ) { | 329 last unless $meta; |
339 return $res; | 330 if ($part =~ /^\d+$/) { |
340 } | 331 $meta = $meta->GetItem($part); |
341 | 332 } else { |
342 my @comp = map { $_, 0 } grep length($_), split(/\.|\[(\d+)\]/, $prefix); | 333 $meta = $meta->GetProperty($part); |
343 | 334 } |
344 return { | 335 } |
345 model => $this->stash->get(['model',0,@comp]), | 336 |
346 prefix => $this->prefix ? $this->prefix . ".$prefix" : $prefix | 337 return $meta; |
347 }; | |
348 } | 338 } |
349 | 339 |
350 sub find_template_for { | 340 sub find_template_for { |
351 my ($this,$model, $nothrow) = @_; | 341 my ($this,$meta, $nothrow) = @_; |
352 | 342 |
353 my $type = typeof($model); | 343 return $this->find_template($meta->template) |
344 if ($meta->template); | |
345 | |
346 my $type = $meta->modelType; | |
354 | 347 |
355 return $this->find_template('templates/plain') unless $type; | 348 return $this->find_template('templates/plain') unless $type; |
356 | 349 |
357 if (my $template = $this->cache->Get($type)) { | 350 if (my $template = $this->cache->Get($type)) { |
358 return $template; | 351 return $template; |
372 | 365 |
373 if ($template) { | 366 if ($template) { |
374 $this->cache->Set($sclass,$template); | 367 $this->cache->Set($sclass,$template); |
375 return $template; | 368 return $template; |
376 } | 369 } |
377 | 370 |
371 #todo $meta->GetISA | |
378 push @isa, @{"${sclass}::ISA"}; | 372 push @isa, @{"${sclass}::ISA"}; |
379 } | 373 } |
380 | 374 |
381 } | 375 } |
382 $this->throw(Template::Constants::ERROR_FILE, "can't find a template for the model " . typeof($model)) | 376 $this->throw(Template::Constants::ERROR_FILE, "can't find a template for the model $type") |
383 unless $nothrow; | 377 unless $nothrow; |
384 | 378 |
385 return; | 379 return; |
386 } | 380 } |
387 | 381 |