Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTDocument.pm @ 192:a9faf883cdce
IMPL::Web::View refactoring
author | cin |
---|---|
date | Fri, 06 Apr 2012 16:22:38 +0400 |
parents | 78a18a2b6266 |
children | 8e8401c0aea4 |
rev | line source |
---|---|
181 | 1 package IMPL::Web::View::TTDocument; |
2 use strict; | |
3 | |
4 use IMPL::lang qw(:declare :constants); | |
5 use IMPL::DOM::Property qw(_dom); | |
6 use IMPL::Web::View::TTFactory(); | |
7 use IMPL::Web::View::TTControl(); | |
8 | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
9 use Scalar::Util qw(weaken); |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
10 |
181 | 11 |
12 use parent qw( | |
13 IMPL::DOM::Document | |
14 IMPL::Web::View::TTControl | |
15 ); | |
16 | |
17 BEGIN { | |
18 public _dom property layout => PROP_ALL; | |
19 public property opts => PROP_GET | PROP_OWNERSET; | |
20 public property loader => PROP_GET | PROP_OWNERSET; | |
21 public property controls => PROP_GET | PROP_OWNERSET; | |
189 | 22 |
23 # store the stash separately to make require() method to work correctly | |
24 # even when a stash of the context is modified during the processing | |
25 public property stash => PROP_GET | PROP_OWNERSET; | |
181 | 26 } |
27 | |
28 sub CTOR { | |
29 my ($this,$template,$refOpts,%args) = @_; | |
30 | |
31 $this->controls({}); | |
32 $this->loader($args{loader}) if $args{loader}; | |
33 | |
34 $this->layout( $template->layout ) unless $this->layout; | |
35 | |
36 $this->opts($refOpts); | |
189 | 37 $this->stash($this->context->stash); |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
38 |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
39 my $self = $this; |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
40 weaken($self); |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
41 |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
42 $this->templateVars('require', sub { |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
43 my $doc = $self; |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
44 die new IMPL::Exception("A document is destroyed or invalid") unless $doc; |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
45 $doc->require(@_); |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
46 }); |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
47 |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
48 $this->templateVars('document', sub { $self } ); |
191 | 49 $this->InitInstance(); |
181 | 50 } |
51 | |
52 our %CTOR = ( | |
53 'IMPL::Web::View::TTControl' => sub { | |
54 'document', | |
55 $_[0], # template | |
56 new Template::Context($_[1]) # context | |
57 }, | |
58 'IMPL::DOM::Document' => sub { | |
59 nodeName => 'document' | |
60 } | |
61 ); | |
62 | |
189 | 63 sub templateVars { |
64 my $this = shift; | |
65 my $name = shift; | |
66 | |
67 if (@_) { | |
68 return $this->stash->set($name, shift); | |
69 } else { | |
70 return $this->stash->get($name); | |
71 } | |
72 } | |
73 | |
181 | 74 sub require { |
192 | 75 my ($this, $control, $nodeProps) = @_; |
76 | |
77 $nodeProps ||= {}; | |
78 $nodeProps->{document} = $this; | |
181 | 79 |
186 | 80 if (my $factory = $this->controls->{$control}) { |
81 return $factory; | |
82 } else { | |
189 | 83 |
186 | 84 my $path = $control; |
181 | 85 if ( my $template = $this->loader->template($path) ) { |
189 | 86 |
191 | 87 my $opts = { %{$this->loader->options} }; |
88 $opts->{STASH} = $this->loader->context->stash->clone(); | |
188 | 89 |
189 | 90 my $ctx = new Template::Context($opts); |
181 | 91 |
186 | 92 $factory = new IMPL::Web::View::TTFactory( |
181 | 93 typeof IMPL::Web::View::TTControl, |
94 $template, | |
95 $ctx, | |
192 | 96 $opts, |
97 { document => $this } | |
181 | 98 ); |
99 | |
186 | 100 my @parts = split(/\/+/,$control); |
181 | 101 |
186 | 102 $this->controls->{$control} = $factory; |
103 | |
104 return $factory; | |
188 | 105 |
181 | 106 } else { |
107 die new IMPL::KeyNotFoundException($control); | |
108 } | |
109 } | |
110 } | |
111 | |
187 | 112 sub renderBlock { |
113 $_[0]->template; | |
114 } | |
115 | |
181 | 116 sub Render { |
187 | 117 my ($this,$args) = @_; |
181 | 118 |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
119 my $output; |
181 | 120 |
121 if ($this->layout) { | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
122 $output = $this->context->include( |
191 | 123 $this->loader->template($this->layout), |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
124 { |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
125 content => sub { $output ||= $this->RenderContent($args); } |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
126 } |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
127 ); |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
128 } else { |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
129 return $this->RenderContent($args); |
181 | 130 } |
131 | |
132 return $output; | |
133 } | |
134 | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
135 sub RenderContent { |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
136 my $this = shift; |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
137 return $this->SUPER::Render(@_); |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
138 } |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
139 |
181 | 140 |
141 1; | |
142 | |
143 __END__ | |
144 | |
145 =pod | |
146 | |
147 =head1 NAME | |
148 | |
149 C<IMPL::Web::View::TTDocument> - документ для построения HTML страницы на основе шаблонов TT. | |
150 | |
151 =head1 SYNOPSIS | |
152 | |
153 =begin code | |
154 | |
155 use IMPL::Web::View::TTDocument(); | |
156 | |
157 my $doc = new IMPL::Wbe::View::TTDocument($template,$ttOptions); | |
158 | |
159 return $doc->Render(); | |
160 | |
161 =end code | |
162 | |
163 Однако, более предпочтительный способ использовать C<IMPL::Web::View::TTLoader>. | |
164 | |
165 =head1 DESCRIPTION | |
166 | |
167 Документ для представления данных. Документы представляют собой иерархически организованные данные, | |
168 элементами данного документа являются данные для отображения, такие как | |
169 | |
170 =over | |
171 | |
172 =item * Объекты из БД | |
173 | |
174 =item * Навигационные цепочки | |
175 | |
176 =item * Меню и т.п. | |
177 | |
178 =back | |
179 | |
180 Скприт шаблона формирует структуру документа, затем сформированная структура форматируется в готовый документ. | |
192 | 181 Процесс форматирования объектной модели в готовый документ может быть выполнена как вручную, так и при помощи |
189 | 182 вспомогательного шаблона - обертки. Если у шаблона документа указан C<layout> в метаданных, то он будет |
181 | 183 использован как шаблон для форматирования объектной модели, вывод самого шаблона будет проигнорирован. Если |
184 обертка не задана, то результатом будет вывод самого скрипта шаблона. | |
185 | |
192 | 186 Использование объектной модели документа позволяет решить задачи по созданию элементов управления |
187 контейнеров, у которых может быть сложное содежимое. Примером таких элементов могут быть формы, | |
188 внутри форм элементы управления могут группироваться. | |
189 | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
190 |
185 | 191 =head2 Порядок обработки документа |
192 | |
193 =over | |
194 | |
195 =item 1 Создается документ при помощи метода C<TTLoader::document()> | |
196 | |
197 =item 1 При создании документа (в конструкторе), происходит выполнение блока C<CTOR> | |
198 | |
199 =item 1 При вызове метода C<TTDocument::Render()> устанавливаются переменные C<this>, C<document> | |
200 и шаблон обрабатывается при помощи метода C<process()> контекста документа. | |
201 | |
202 =back | |
181 | 203 |
204 =head2 Загрузка элемента управления | |
205 | |
206 =over | |
207 | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
208 =item 1 C<require('my/org/input')> |
181 | 209 |
210 =item 1 Загружает шаблон C<my/org/input.tt> | |
211 | |
189 | 212 =item 1 Создает фабрику элементов управления с собственным контекстом, унаследованным от контекст документа. |
181 | 213 |
189 | 214 =item 1 Выполняет шаблон в пространстве имен фабрики |
181 | 215 |
216 =back | |
217 | |
218 =head2 Создание элемента управления | |
219 | |
220 =over | |
221 | |
222 =item 1 C<< my.org.input.new('login') >> | |
223 | |
224 =item 1 Если это первый элемент управления, то выполняетя статический конструктор в контексте фабрики | |
225 | |
226 =item 1 Создается новый дочерний контекст к контексту фабрики | |
227 | |
228 =item 1 Создается экземпляр элемента управления | |
229 | |
230 =item 1 Выполняется блок конструктора в контексте элемента управления, параметр C<this> имеет значение | |
231 нового экземпляра элемента управления | |
232 | |
233 =back | |
234 | |
235 =head1 MEMBERS | |
236 | |
237 =over | |
238 | |
239 =item C<CTOR($template, %options)> | |
240 | |
241 Создает экземпляр документа с указанным шаблоном и параметрами, параметры | |
242 | |
243 =back | |
244 | |
245 =cut |