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