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