Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTDocument.pm @ 190:cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
author | cin |
---|---|
date | Wed, 04 Apr 2012 17:51:27 +0400 |
parents | 08015e2803f1 |
children | 78a18a2b6266 |
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 } ); |
181 | 49 } |
50 | |
51 our %CTOR = ( | |
52 'IMPL::Web::View::TTControl' => sub { | |
53 'document', | |
54 $_[0], # template | |
55 new Template::Context($_[1]) # context | |
56 }, | |
57 'IMPL::DOM::Document' => sub { | |
58 nodeName => 'document' | |
59 } | |
60 ); | |
61 | |
189 | 62 sub templateVars { |
63 my $this = shift; | |
64 my $name = shift; | |
65 | |
66 if (@_) { | |
67 return $this->stash->set($name, shift); | |
68 } else { | |
69 return $this->stash->get($name); | |
70 } | |
71 } | |
72 | |
181 | 73 sub require { |
74 my ($this, $control) = @_; | |
75 | |
186 | 76 if (my $factory = $this->controls->{$control}) { |
77 return $factory; | |
78 } else { | |
189 | 79 |
186 | 80 my $path = $control; |
181 | 81 if ( my $template = $this->loader->template($path) ) { |
189 | 82 |
83 my $opts = { %{$this->opts} }; | |
84 $opts->{STASH} = $this->stash->clone(); | |
188 | 85 |
189 | 86 my $ctx = new Template::Context($opts); |
181 | 87 |
186 | 88 $factory = new IMPL::Web::View::TTFactory( |
181 | 89 typeof IMPL::Web::View::TTControl, |
90 $template, | |
91 $ctx, | |
189 | 92 $opts |
181 | 93 ); |
94 | |
186 | 95 my @parts = split(/\/+/,$control); |
181 | 96 |
186 | 97 $this->controls->{$control} = $factory; |
98 | |
99 return $factory; | |
188 | 100 |
181 | 101 } else { |
102 die new IMPL::KeyNotFoundException($control); | |
103 } | |
104 } | |
105 } | |
106 | |
187 | 107 sub renderBlock { |
108 $_[0]->template; | |
109 } | |
110 | |
181 | 111 sub Render { |
187 | 112 my ($this,$args) = @_; |
181 | 113 |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
114 my $output; |
181 | 115 |
116 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
|
117 $output = $this->context->include( |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
118 $this->layout, |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
119 { |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
120 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
|
121 } |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
122 ); |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
123 } else { |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
124 return $this->RenderContent($args); |
181 | 125 } |
126 | |
127 return $output; | |
128 } | |
129 | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
130 sub RenderContent { |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
131 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
|
132 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
|
133 } |
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
134 |
181 | 135 |
136 1; | |
137 | |
138 __END__ | |
139 | |
140 =pod | |
141 | |
142 =head1 NAME | |
143 | |
144 C<IMPL::Web::View::TTDocument> - документ для построения HTML страницы на основе шаблонов TT. | |
145 | |
146 =head1 SYNOPSIS | |
147 | |
148 =begin code | |
149 | |
150 use IMPL::Web::View::TTDocument(); | |
151 | |
152 my $doc = new IMPL::Wbe::View::TTDocument($template,$ttOptions); | |
153 | |
154 return $doc->Render(); | |
155 | |
156 =end code | |
157 | |
158 Однако, более предпочтительный способ использовать C<IMPL::Web::View::TTLoader>. | |
159 | |
160 =head1 DESCRIPTION | |
161 | |
162 Документ для представления данных. Документы представляют собой иерархически организованные данные, | |
163 элементами данного документа являются данные для отображения, такие как | |
164 | |
165 =over | |
166 | |
167 =item * Объекты из БД | |
168 | |
169 =item * Навигационные цепочки | |
170 | |
171 =item * Меню и т.п. | |
172 | |
173 =back | |
174 | |
175 Скприт шаблона формирует структуру документа, затем сформированная структура форматируется в готовый документ. | |
176 Процесс преобразования объектной модели в готовый документ может быть выполнена как вручную, так и при помощи | |
189 | 177 вспомогательного шаблона - обертки. Если у шаблона документа указан C<layout> в метаданных, то он будет |
181 | 178 использован как шаблон для форматирования объектной модели, вывод самого шаблона будет проигнорирован. Если |
179 обертка не задана, то результатом будет вывод самого скрипта шаблона. | |
180 | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
181 |
185 | 182 =head2 Порядок обработки документа |
183 | |
184 =over | |
185 | |
186 =item 1 Создается документ при помощи метода C<TTLoader::document()> | |
187 | |
188 =item 1 При создании документа (в конструкторе), происходит выполнение блока C<CTOR> | |
189 | |
190 =item 1 При вызове метода C<TTDocument::Render()> устанавливаются переменные C<this>, C<document> | |
191 и шаблон обрабатывается при помощи метода C<process()> контекста документа. | |
192 | |
193 =back | |
181 | 194 |
195 =head2 Загрузка элемента управления | |
196 | |
197 =over | |
198 | |
190
cd1ff7029a63
IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents:
189
diff
changeset
|
199 =item 1 C<require('my/org/input')> |
181 | 200 |
201 =item 1 Загружает шаблон C<my/org/input.tt> | |
202 | |
189 | 203 =item 1 Создает фабрику элементов управления с собственным контекстом, унаследованным от контекст документа. |
181 | 204 |
189 | 205 =item 1 Выполняет шаблон в пространстве имен фабрики |
181 | 206 |
207 =back | |
208 | |
209 =head2 Создание элемента управления | |
210 | |
211 =over | |
212 | |
213 =item 1 C<< my.org.input.new('login') >> | |
214 | |
215 =item 1 Если это первый элемент управления, то выполняетя статический конструктор в контексте фабрики | |
216 | |
217 =item 1 Создается новый дочерний контекст к контексту фабрики | |
218 | |
219 =item 1 Создается экземпляр элемента управления | |
220 | |
221 =item 1 Выполняется блок конструктора в контексте элемента управления, параметр C<this> имеет значение | |
222 нового экземпляра элемента управления | |
223 | |
224 =back | |
225 | |
226 =head1 MEMBERS | |
227 | |
228 =over | |
229 | |
230 =item C<CTOR($template, %options)> | |
231 | |
232 Создает экземпляр документа с указанным шаблоном и параметрами, параметры | |
233 | |
234 =back | |
235 | |
236 =cut |