Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTDocument.pm @ 307:2da2564f115d
*TTView: fixed memory leak
author | cin |
---|---|
date | Thu, 18 Apr 2013 02:21:28 +0400 |
parents | b5d5793f348e |
children | 47a09a8dc23a |
rev | line source |
---|---|
181 | 1 package IMPL::Web::View::TTDocument; |
2 use strict; | |
3 | |
236 | 4 use Scalar::Util qw(weaken); |
5 use IMPL::Const qw(:prop); | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
6 use IMPL::lang qw(:hash is); |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
7 use Carp qw(carp); |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
8 use mro; |
181 | 9 |
236 | 10 use IMPL::declare { |
11 require => { | |
305 | 12 TTRegistry => 'IMPL::Web::View::TTRegistry', |
236 | 13 TTFactory => 'IMPL::Web::View::TTFactory', |
14 TTControl => 'IMPL::Web::View::TTControl', | |
15 Loader => 'IMPL::Code::Loader' | |
16 }, | |
17 base => [ | |
18 'IMPL::Web::View::TTControl' => sub { | |
287 | 19 my ($template,$ctx) = @_; |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
20 $ctx ||= Template::Context->new(); |
300 | 21 return $template, $ctx; # context |
236 | 22 } |
23 ], | |
24 props => [ | |
25 layout => PROP_RW, | |
26 loader => PROP_RW, | |
307 | 27 baseLocation => PROP_RW |
236 | 28 ] |
29 }; | |
181 | 30 |
31 sub CTOR { | |
287 | 32 my ($this,$template,$ctx,$loader,$vars) = @_; |
194 | 33 |
34 $this->loader($loader) if $loader; | |
35 $this->layout( $template->layout ) unless $this->layout; | |
301 | 36 $this->title( $template->title ) unless $this->title; |
194 | 37 |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
38 $this->context->stash->update($vars) |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
39 if ref $vars eq 'HASH'; |
181 | 40 } |
41 | |
42 sub Render { | |
194 | 43 my ($this,$args) = @_; |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
44 |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
45 $args ||= {}; |
290 | 46 |
305 | 47 $this->context->localise(); # localise stash |
48 my $documentContext = _clone_context( $this->context ); # create a new context | |
290 | 49 |
305 | 50 my $registry = TTRegistry->new($this->loader, $documentContext); |
51 | |
52 $this->context->stash->update({ | |
53 require => sub { $registry->Require(shift) }, | |
307 | 54 document => sub { $this } |
305 | 55 }); |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
56 |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
57 my $text = eval { |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
58 |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
59 if ($this->layout) { |
287 | 60 my $tlayout = $this->loader->layout($this->layout); |
61 if(my $init = $tlayout->blocks->{INIT}) { | |
62 $this->context->process( | |
63 $init, | |
64 hashMerge( | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
65 $args, |
287 | 66 { |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
67 template => $this->template |
287 | 68 } |
69 ) | |
70 ); | |
71 } | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
72 my $content = $this->next::method($args); |
287 | 73 return $this->context->include( |
74 $tlayout, | |
75 { | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
76 %{$args}, |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
77 content => $content, |
287 | 78 this => $this, |
79 template => $this->template | |
80 } | |
286 | 81 ); |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
82 } else { |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
83 return $this->next::method($args); |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
84 } |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
85 }; |
301 | 86 |
87 my $e = $@; | |
289 | 88 |
305 | 89 $registry->Dispose(); |
90 undef $registry; | |
289 | 91 $this->context->delocalise(); |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
92 |
307 | 93 undef $this; |
94 | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
95 if ($e) { |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
96 die $e; |
194 | 97 } else { |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
98 return $text; |
194 | 99 } |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
100 } |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
101 |
303
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
102 sub GetTemplate { |
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
103 my ($this,$name) = @_; |
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
104 |
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
105 $this->template->blocks->{$name}; |
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
106 } |
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
107 |
289 | 108 sub _clone_context { |
109 my $args = { %{shift || {}} }; | |
110 delete $args->{CONFIG}; | |
111 | |
112 return Template::Context->new($args); | |
113 } | |
114 | |
181 | 115 1; |
116 | |
117 __END__ | |
118 | |
119 =pod | |
120 | |
121 =head1 NAME | |
122 | |
123 C<IMPL::Web::View::TTDocument> - документ для построения HTML страницы на основе шаблонов TT. | |
124 | |
125 =head1 SYNOPSIS | |
126 | |
127 =begin code | |
128 | |
129 | |
130 =end code | |
131 | |
132 =head1 DESCRIPTION | |
133 | |
134 | |
135 =over | |
136 | |
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
137 |
181 | 138 =cut |