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