comparison 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
comparison
equal deleted inserted replaced
353:feeb3bc4a818 354:9330835535b9
32 prefix 32 prefix
33 cache 33 cache
34 includes 34 includes
35 modules 35 modules
36 aliases 36 aliases
37 resolver
37 )) { 38 )) {
38 my $t = $prop; 39 my $t = $prop;
39 40
40 *{__PACKAGE__ . '::' . $prop} = sub { 41 *{__PACKAGE__ . '::' . $prop} = sub {
41 my $this = shift; 42 my $this = shift;
135 $args = shift; 136 $args = shift;
136 } 137 }
137 138
138 my $prefix = $this->prefix; 139 my $prefix = $this->prefix;
139 140
141 my $info;
142
140 if (not(($args and delete $args->{_no_resolve}) or ref $model)) { 143 if (not(($args and delete $args->{_no_resolve}) or ref $model)) {
141 $prefix = $prefix ? "${prefix}.${model}" : $model; 144 $info = $this->resolve_model($model,$args);
142 $model = $this->resolve_model($model);
143 } else { 145 } else {
144 $prefix = ""; 146 $info = {
145 } 147 model => $model,
146 148 prefix => ""
147 $template = $template ? $this->find_template($template) : $this->find_template_for($model); 149 };
150 }
151
152 $template = $template ? $this->find_template($template) : $this->find_template_for($info->{model});
148 153
149 return $this->render( 154 return $this->render(
150 $template, 155 $template,
151 hashApply( 156 hashApply(
152 { 157 $info,
153 prefix => $prefix,
154 model => $model,
155 },
156 $args 158 $args
157 ) 159 )
158 ); 160 );
159 } 161 }
160 162
195 sub invoke_environment { 197 sub invoke_environment {
196 my ($this,$code,$env) = @_; 198 my ($this,$code,$env) = @_;
197 199
198 $env ||= {}; 200 $env ||= {};
199 201
202 my $ctx = ($this->root || $this)->clone();
203
200 my $out = eval { 204 my $out = eval {
201 $this->localise( 205 $ctx->localise(
202 hashApply( 206 hashApply(
203 { 207 {
204 aliases => $this->aliases || {}, 208 aliases => $this->aliases || {},
205 root => $this->root || $this, 209 root => $this->root || $ctx,
206 modules => $this->modules || {}, 210 modules => $this->modules || {},
207 cache => TypeKeyedCollection->new(), 211 cache => TypeKeyedCollection->new(),
208 display => sub { 212 display => sub {
209 $this->display(@_); 213 $ctx->display(@_);
210 }, 214 },
211 render => sub { 215 render => sub {
212 $this->render(@_); 216 $ctx->render(@_);
213 }, 217 },
214 display_model => sub { 218 display_model => sub {
215 $this->display_model(@_); 219 $ctx->display_model(@_);
216 }, 220 },
217 tt_cache => {} 221 tt_cache => {}
218 }, 222 },
219 $env 223 $env
220 ) 224 )
221 ); 225 );
222 226
223 &$code($this); 227 &$code($ctx);
224 }; 228 };
225 229
226 my $e = $@; 230 my $e = $@;
227 $this->delocalise(); 231 $ctx->delocalise();
228 232
229 die $e if $e; 233 die $e if $e;
230 234
231 return $out; 235 return $out;
232 } 236 }
277 ) 281 )
278 ) 282 )
279 } 283 }
280 284
281 sub resolve_model { 285 sub resolve_model {
282 my ($this,$prefix) = @_; 286 my ($this,$prefix,$args) = @_;
283 287
284 die ArgException->new(prefix => "the prefix must be specified") 288 die ArgException->new(prefix => "the prefix must be specified")
285 unless defined $prefix; 289 unless defined $prefix;
286 290
287 #TODO handle DOM models 291 #TODO handle DOM models
288 292
293 if (my $resolver = $this->resolver) {
294 return $this->$resolver($prefix,$args);
295 }
296
289 my @comp = map { $_, 0 } grep length($_), split(/\.|\[(\d+)\]/, $prefix); 297 my @comp = map { $_, 0 } grep length($_), split(/\.|\[(\d+)\]/, $prefix);
290 298
291 return $this->stash->get(['model',0,@comp]); 299 return {
300 model => $this->stash->get(['model',0,@comp]),
301 prefix => $this->prefix ? $this->prefix . ".$prefix" : $prefix
302 };
292 } 303 }
293 304
294 sub find_template_for { 305 sub find_template_for {
295 my ($this,$model) = @_; 306 my ($this,$model) = @_;
296 307