Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTDocument.pm @ 343:9bdccdf1f50b
Added a templates context
author | cin |
---|---|
date | Fri, 13 Sep 2013 12:53:15 +0400 |
parents | ec4ec1f056fe |
children | f1d67615a5b1 |
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, | |
311 | 27 layoutBase => PROP_RW, |
28 registry => PROP_RW, | |
307 | 29 baseLocation => PROP_RW |
236 | 30 ] |
31 }; | |
181 | 32 |
33 sub CTOR { | |
311 | 34 my ($this,$template,$ctx) = @_; |
194 | 35 |
36 $this->layout( $template->layout ) unless $this->layout; | |
301 | 37 $this->title( $template->title ) unless $this->title; |
311 | 38 my $doc = $this; |
39 weaken($doc); | |
40 $this->registry->context->stash->update({ | |
41 document => sub { $doc } | |
42 }); | |
181 | 43 } |
44 | |
45 sub Render { | |
194 | 46 my ($this,$args) = @_; |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
47 |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
48 $args ||= {}; |
310
0a9d51cf6dfd
*TTView: refactoring, document supports custom classes, layouts are become controls
sergey
parents:
309
diff
changeset
|
49 $args->{document} = $this; |
290 | 50 |
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
51 |
313
ec4ec1f056fe
TTView: page content now rendered lazy therefore a page layout must wrap the document content
cin
parents:
311
diff
changeset
|
52 if ($this->layout) { |
ec4ec1f056fe
TTView: page content now rendered lazy therefore a page layout must wrap the document content
cin
parents:
311
diff
changeset
|
53 my $layout = $this->registry->Require(join('/',$this->layoutBase, $this->layout))->new(); |
ec4ec1f056fe
TTView: page content now rendered lazy therefore a page layout must wrap the document content
cin
parents:
311
diff
changeset
|
54 |
ec4ec1f056fe
TTView: page content now rendered lazy therefore a page layout must wrap the document content
cin
parents:
311
diff
changeset
|
55 my $next = $this->next::can(); |
ec4ec1f056fe
TTView: page content now rendered lazy therefore a page layout must wrap the document content
cin
parents:
311
diff
changeset
|
56 |
ec4ec1f056fe
TTView: page content now rendered lazy therefore a page layout must wrap the document content
cin
parents:
311
diff
changeset
|
57 return $layout->Render({ |
ec4ec1f056fe
TTView: page content now rendered lazy therefore a page layout must wrap the document content
cin
parents:
311
diff
changeset
|
58 content => sub { $this->$next($args) }, |
ec4ec1f056fe
TTView: page content now rendered lazy therefore a page layout must wrap the document content
cin
parents:
311
diff
changeset
|
59 template => $this->template, |
ec4ec1f056fe
TTView: page content now rendered lazy therefore a page layout must wrap the document content
cin
parents:
311
diff
changeset
|
60 document => $this |
ec4ec1f056fe
TTView: page content now rendered lazy therefore a page layout must wrap the document content
cin
parents:
311
diff
changeset
|
61 }); |
194 | 62 } else { |
313
ec4ec1f056fe
TTView: page content now rendered lazy therefore a page layout must wrap the document content
cin
parents:
311
diff
changeset
|
63 return $this->next::method($args); |
194 | 64 } |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
65 } |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
66 |
303
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
67 sub GetTemplate { |
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
68 my ($this,$name) = @_; |
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
69 |
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
70 $this->template->blocks->{$name}; |
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
71 } |
a5eb64c6e6f7
TTDocument.GetTemplate corrected to work with document blocks
cin
parents:
301
diff
changeset
|
72 |
308 | 73 sub DTOR { |
74 my $this = shift; | |
75 | |
76 $this->registry->Dispose() if $this->registry; | |
77 } | |
78 | |
181 | 79 1; |
80 | |
81 __END__ | |
82 | |
83 =pod | |
84 | |
85 =head1 NAME | |
86 | |
87 C<IMPL::Web::View::TTDocument> - документ для построения HTML страницы на основе шаблонов TT. | |
88 | |
89 =head1 SYNOPSIS | |
90 | |
91 =begin code | |
92 | |
93 | |
94 =end code | |
95 | |
96 =head1 DESCRIPTION | |
97 | |
343 | 98 Позволяет строить представления при помощи Template Toolkit, при этом расширяет |
99 шаблоны до элементов управления, чтобы была возмлжность реализации функционала | |
100 средствами C<Perl>. | |
101 | |
102 Структура представления. | |
103 | |
104 =begin text | |
105 | |
106 + view | |
107 |- document.tt | |
108 |-+items | |
109 | |- title.tt | |
110 | |- item.tt | |
111 | | |
112 |-+layouts | |
113 |- | |
114 | |
115 =end text | |
116 | |
117 =begin text | |
118 | |
119 | |
120 | |
121 =end text | |
122 | |
181 | 123 |
124 =over | |
125 | |
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
126 |
181 | 127 =cut |