Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTDocument.pm @ 309:5e4e7c8fbca1
sync
author | cin |
---|---|
date | Fri, 19 Apr 2013 00:27:51 +0400 |
parents | 47a09a8dc23a |
children | 0a9d51cf6dfd |
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; |
309 | 9 use IMPL::Exception(); |
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', | |
309 | 15 Loader => 'IMPL::Code::Loader', |
16 OpException => '-IMPL::InvalidOperationException' | |
236 | 17 }, |
18 base => [ | |
19 'IMPL::Web::View::TTControl' => sub { | |
309 | 20 my ($template,$ctx,$vars) = @_; |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
21 $ctx ||= Template::Context->new(); |
309 | 22 return $template, $ctx, $vars; # context |
236 | 23 } |
24 ], | |
25 props => [ | |
26 layout => PROP_RW, | |
307 | 27 baseLocation => PROP_RW |
236 | 28 ] |
29 }; | |
181 | 30 |
31 sub CTOR { | |
308 | 32 my ($this,$template,$ctx,$vars) = @_; |
194 | 33 |
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 | |
48 | |
49 $this->context->stash->update({ | |
307 | 50 document => sub { $this } |
305 | 51 }); |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
52 |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
53 my $text = eval { |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
54 |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
55 if ($this->layout) { |
309 | 56 my $tlayout = $this->registry->loader->layout($this->layout) |
57 or die OpException->new('The specified layout isn\'t found', $this->layout); | |
287 | 58 if(my $init = $tlayout->blocks->{INIT}) { |
59 $this->context->process( | |
60 $init, | |
61 hashMerge( | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
62 $args, |
287 | 63 { |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
64 template => $this->template |
287 | 65 } |
66 ) | |
67 ); | |
68 } | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
69 my $content = $this->next::method($args); |
287 | 70 return $this->context->include( |
71 $tlayout, | |
72 { | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
73 %{$args}, |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
74 content => $content, |
287 | 75 this => $this, |
76 template => $this->template | |
77 } | |
286 | 78 ); |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
79 } else { |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
80 return $this->next::method($args); |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
81 } |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
82 }; |
301 | 83 |
84 my $e = $@; | |
289 | 85 |
86 $this->context->delocalise(); | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
87 |
307 | 88 undef $this; |
89 | |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
90 if ($e) { |
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
91 die $e; |
194 | 92 } else { |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
93 return $text; |
194 | 94 } |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
95 } |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
96 |
303
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
97 sub GetTemplate { |
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
98 my ($this,$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 $this->template->blocks->{$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 |
308 | 103 sub DTOR { |
104 my $this = shift; | |
105 | |
106 $this->registry->Dispose() if $this->registry; | |
107 } | |
108 | |
289 | 109 sub _clone_context { |
110 my $args = { %{shift || {}} }; | |
111 delete $args->{CONFIG}; | |
112 | |
113 return Template::Context->new($args); | |
114 } | |
115 | |
181 | 116 1; |
117 | |
118 __END__ | |
119 | |
120 =pod | |
121 | |
122 =head1 NAME | |
123 | |
124 C<IMPL::Web::View::TTDocument> - документ для построения HTML страницы на основе шаблонов TT. | |
125 | |
126 =head1 SYNOPSIS | |
127 | |
128 =begin code | |
129 | |
130 | |
131 =end code | |
132 | |
133 =head1 DESCRIPTION | |
134 | |
135 | |
136 =over | |
137 | |
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
138 |
181 | 139 =cut |