annotate Lib/IMPL/Web/View/TTDocument.pm @ 189:08015e2803f1

IMPL::Vew::Web - fixed memory leaks, more tests
author cin
date Wed, 04 Apr 2012 02:49:45 +0400
parents 029c9610528c
children cd1ff7029a63
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
1 package IMPL::Web::View::TTDocument;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
2 use strict;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
3
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
4 use IMPL::lang qw(:declare :constants);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
5 use IMPL::DOM::Property qw(_dom);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
6 use IMPL::Web::View::TTFactory();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
7 use IMPL::Web::View::TTControl();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
8
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
9
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
10 use parent qw(
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
11 IMPL::DOM::Document
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
12 IMPL::Web::View::TTControl
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
13 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
14
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
15 BEGIN {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
16 public _dom property layout => PROP_ALL;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
17 public property opts => PROP_GET | PROP_OWNERSET;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
18 public property loader => PROP_GET | PROP_OWNERSET;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
19 public property controls => PROP_GET | PROP_OWNERSET;
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
20
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
21 # store the stash separately to make require() method to work correctly
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
22 # even when a stash of the context is modified during the processing
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
23 public property stash => PROP_GET | PROP_OWNERSET;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
24 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
25
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
26 sub CTOR {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
27 my ($this,$template,$refOpts,%args) = @_;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
28
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
29 $this->controls({});
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
30 $this->loader($args{loader}) if $args{loader};
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
31
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
32 $this->layout( $template->layout ) unless $this->layout;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
33
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
34 $this->opts($refOpts);
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
35 $this->stash($this->context->stash);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
36 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
37
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
38 our %CTOR = (
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
39 'IMPL::Web::View::TTControl' => sub {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
40 'document',
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
41 $_[0], # template
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
42 new Template::Context($_[1]) # context
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
43 },
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
44 'IMPL::DOM::Document' => sub {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
45 nodeName => 'document'
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
46 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
47 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
48
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
49 sub templateVars {
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
50 my $this = shift;
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
51 my $name = shift;
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
52
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
53 if (@_) {
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
54 return $this->stash->set($name, shift);
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
55 } else {
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
56 return $this->stash->get($name);
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
57 }
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
58 }
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
59
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
60 sub require {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
61 my ($this, $control) = @_;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
62
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
63 if (my $factory = $this->controls->{$control}) {
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
64 return $factory;
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
65 } else {
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
66
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
67 my $path = $control;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
68 if ( my $template = $this->loader->template($path) ) {
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
69
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
70 my $opts = { %{$this->opts} };
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
71 $opts->{STASH} = $this->stash->clone();
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 187
diff changeset
72
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
73 my $ctx = new Template::Context($opts);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
74
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
75 $factory = new IMPL::Web::View::TTFactory(
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
76 typeof IMPL::Web::View::TTControl,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
77 $template,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
78 $ctx,
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
79 $opts
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
80 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
81
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
82 my @parts = split(/\/+/,$control);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
83
186
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
84 $this->controls->{$control} = $factory;
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
85
6c0fee769b0c IMPL::Web::View::TTControl tests, fixes
cin
parents: 185
diff changeset
86 return $factory;
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 187
diff changeset
87
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
88 } else {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
89 die new IMPL::KeyNotFoundException($control);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
90 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
91 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
92 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
93
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
94 sub renderBlock {
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
95 $_[0]->template;
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
96 }
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
97
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
98 sub Render {
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
99 my ($this,$args) = @_;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
100
188
029c9610528c Memory leak tests in IMPL::Web::View
cin
parents: 187
diff changeset
101 my $output = $this->SUPER::Render( { document => $this } );
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
102
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
103 if ($this->layout) {
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
104 $output = $this->context->include($this->layout, { content => $output } );
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
105 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
106
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
107 return $output;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
108 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
109
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
110
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
111 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
112
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
113 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
114
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
115 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
116
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
117 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
118
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
119 C<IMPL::Web::View::TTDocument> - документ для построения HTML страницы на основе шаблонов TT.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
120
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
121 =head1 SYNOPSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
122
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
123 =begin code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
124
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
125 use IMPL::Web::View::TTDocument();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
126
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
127 my $doc = new IMPL::Wbe::View::TTDocument($template,$ttOptions);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
128
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
129 return $doc->Render();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
130
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
131 =end code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
132
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
133 Однако, более предпочтительный способ использовать C<IMPL::Web::View::TTLoader>.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
134
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
135 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
136
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
137 Документ для представления данных. Документы представляют собой иерархически организованные данные,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
138 элементами данного документа являются данные для отображения, такие как
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
139
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
140 =over
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
141
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
142 =item * Объекты из БД
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
143
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
144 =item * Навигационные цепочки
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
145
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
146 =item * Меню и т.п.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
147
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
148 =back
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
149
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
150 Скприт шаблона формирует структуру документа, затем сформированная структура форматируется в готовый документ.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
151 Процесс преобразования объектной модели в готовый документ может быть выполнена как вручную, так и при помощи
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
152 вспомогательного шаблона - обертки. Если у шаблона документа указан C<layout> в метаданных, то он будет
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
153 использован как шаблон для форматирования объектной модели, вывод самого шаблона будет проигнорирован. Если
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
154 обертка не задана, то результатом будет вывод самого скрипта шаблона.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
155
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
156 Каждый документ имеет свое собственное пространство имен, которое может быть вложенным в некоторое внешнее,
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
157 указанное при создании документа.
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
158 =head2 Порядок обработки документа
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
159
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
160 =over
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
161
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
162 =item 1 Создается документ при помощи метода C<TTLoader::document()>
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
163
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
164 =item 1 При создании документа (в конструкторе), происходит выполнение блока C<CTOR>
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
165
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
166 =item 1 При вызове метода C<TTDocument::Render()> устанавливаются переменные C<this>, C<document>
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
167 и шаблон обрабатывается при помощи метода C<process()> контекста документа.
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
168
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
169 =back
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
170
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
171 =head2 Загрузка элемента управления
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
172
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
173 =over
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
174
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
175 =item 1 C<document.require('my/org/input')>
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
176
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
177 =item 1 Загружает шаблон C<my/org/input.tt>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
178
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
179 =item 1 Создает фабрику элементов управления с собственным контекстом, унаследованным от контекст документа.
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
180
189
08015e2803f1 IMPL::Vew::Web - fixed memory leaks, more tests
cin
parents: 188
diff changeset
181 =item 1 Выполняет шаблон в пространстве имен фабрики
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
182
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
183 =back
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
184
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
185 =head2 Создание элемента управления
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
186
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
187 =over
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
188
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
189 =item 1 C<< my.org.input.new('login') >>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
190
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
191 =item 1 Если это первый элемент управления, то выполняетя статический конструктор в контексте фабрики
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
192
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
193 =item 1 Создается новый дочерний контекст к контексту фабрики
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
194
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
195 =item 1 Создается экземпляр элемента управления
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
196
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
197 =item 1 Выполняется блок конструктора в контексте элемента управления, параметр C<this> имеет значение
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
198 нового экземпляра элемента управления
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
199
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
200 =back
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
201
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
202 =head1 MEMBERS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
203
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
204 =over
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
205
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
206 =item C<CTOR($template, %options)>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
207
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
208 Создает экземпляр документа с указанным шаблоном и параметрами, параметры
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
209
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
210 =back
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
211
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
212 =cut