annotate Lib/IMPL/Web/View/TTControl.pm @ 301:aeeb57a12046

*IMPL::Web::View: templates inheritance support
author cin
date Mon, 25 Mar 2013 02:04:18 +0400
parents bf3af33b9003
children 673581380e79
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::TTControl;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
2 use strict;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
3
234
sergey
parents: 232
diff changeset
4 use IMPL::Const qw(:prop);
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
5 use IMPL::lang qw(:hash);
299
sergey
parents: 296
diff changeset
6 use Scalar::Util qw(blessed reftype);
234
sergey
parents: 232
diff changeset
7 use IMPL::declare {
sergey
parents: 232
diff changeset
8 require => {
296
cin
parents: 267
diff changeset
9 TemplateDocument => 'Template::Document',
234
sergey
parents: 232
diff changeset
10 TTContext => 'Template::Context',
sergey
parents: 232
diff changeset
11 Exception => 'IMPL::Exception',
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
12 ArgumentException => '-IMPL::InvalidArgumentException',
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
13 OperationException => '-IMPL::InvalidOperationException'
234
sergey
parents: 232
diff changeset
14 },
sergey
parents: 232
diff changeset
15 base => [
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
16 'IMPL::Object' => undef
234
sergey
parents: 232
diff changeset
17 ],
sergey
parents: 232
diff changeset
18 props => [
sergey
parents: 232
diff changeset
19 id => PROP_RO,
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
20 attributes => PROP_RW,
234
sergey
parents: 232
diff changeset
21 context => PROP_RO,
sergey
parents: 232
diff changeset
22 template => PROP_RO
sergey
parents: 232
diff changeset
23 ]
sergey
parents: 232
diff changeset
24 };
sergey
parents: 232
diff changeset
25
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
26
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
27 {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
28 my $nextId = 1;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
29 sub _GetNextId {
299
sergey
parents: 296
diff changeset
30 return '_' . $nextId++;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
31 }
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
32 }
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
33
300
cin
parents: 299
diff changeset
34 our $AUTOLOAD_REGEX = qr/^[a-z]/;
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
35
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
36 sub CTOR {
299
sergey
parents: 296
diff changeset
37 my ($this,$template,$context,$attrs) = @_;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
38
299
sergey
parents: 296
diff changeset
39
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
40 $this->template( $template ) or die new IMPL::ArgumentException("A template is required");
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
41 $this->context( $context ) or die new IMPL::ArgumentException("A context is required");
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
42
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
43 $this->attributes({});
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
44
301
aeeb57a12046 *IMPL::Web::View: templates inheritance support
cin
parents: 300
diff changeset
45 if(ref($attrs) eq 'HASH') {
299
sergey
parents: 296
diff changeset
46 while (my($key,$value) = each %$attrs) {
sergey
parents: 296
diff changeset
47 $this->SetAttribute($key,$value);
sergey
parents: 296
diff changeset
48 }
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
49 }
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
50
299
sergey
parents: 296
diff changeset
51 $this->id(_GetNextId()) unless $this->id;
191
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 189
diff changeset
52 }
78a18a2b6266 IMPL::Web::View improvements (unstable)
cin
parents: 189
diff changeset
53
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
54 sub GetAttribute {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
55 my ($this,$name) = (shift,shift);
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
56
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
57 if (my $method = $this->can($name)) {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
58 unshift @_,$this;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
59 goto &$method;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
60 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
61 return $this->attributes->{$name};
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
62 }
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
63 }
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
64
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
65 sub SetAttribute {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
66 my $this = shift;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
67 my $name = shift;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
68
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
69 if (my $method = $this->can($name)) {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
70 unshift @_, $this;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
71 goto &$method;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
72 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
73 return $this->attributes->{$name} = shift;
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
74 }
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
75 }
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
76
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
77 sub Render {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
78 my ($this,$args) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
79
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
80 $args = {} unless ref $args eq 'HASH';
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
81
299
sergey
parents: 296
diff changeset
82 return $this->context->include(
301
aeeb57a12046 *IMPL::Web::View: templates inheritance support
cin
parents: 300
diff changeset
83 $this->template->block,
299
sergey
parents: 296
diff changeset
84 {
sergey
parents: 296
diff changeset
85 %$args,
sergey
parents: 296
diff changeset
86 this => $this,
sergey
parents: 296
diff changeset
87 template => $this->template
sergey
parents: 296
diff changeset
88 }
sergey
parents: 296
diff changeset
89 );
267
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 265
diff changeset
90 }
bbc0da7ef90e *IMPL::Web::View refactoring
cin
parents: 265
diff changeset
91
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 181
diff changeset
92 sub AUTOLOAD {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
93 our $AUTOLOAD;
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
94
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
95 my $method = ($AUTOLOAD =~ m/(\w+)$/)[0];
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
96
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
97 return if $method eq 'DESTROY';
4d0e1962161c Replaced tabs with spaces
cin
parents: 192
diff changeset
98
300
cin
parents: 299
diff changeset
99 if ($method =~ /$AUTOLOAD_REGEX/) {
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
100 my $this = shift;
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
101
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
102 die OperationException->new("can't invoke method '$method' on an unblessed reference") unless blessed $this;
238
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
103
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
104 return @_ ? $this->SetAttribute($method,@_) : $this->GetAttribute($method);
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
105 } else {
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
106 die OperationException->new("The specified method '$method' doesn't exists");
b8c724f6de36 DOM model refactoring
sergey
parents: 236
diff changeset
107 }
181
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 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
111
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
112 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
113
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
114 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
115
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
116 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
117
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
118 C<IMPL::Web::View::TTControl>
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
119
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
120 =head1 SYNPOSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
121
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
122 =begin text
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
123
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
124 [%
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
125 META version = 1;
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
126 BLOCK INIT;
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
127 # this is a document scope
299
sergey
parents: 296
diff changeset
128 dojo.modules.push( 'dijit/form/Input' );
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
129 END;
299
sergey
parents: 296
diff changeset
130
sergey
parents: 296
diff changeset
131 # local to this block
sergey
parents: 296
diff changeset
132 TPreview = require('My/Org/TextPreview');
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
133
299
sergey
parents: 296
diff changeset
134 # init control props
sergey
parents: 296
diff changeset
135 visualClass = this.visualClass || 'classic';
sergey
parents: 296
diff changeset
136 %]
sergey
parents: 296
diff changeset
137 <div id="$id" class="$visualClass" data-dojo-type="dijit/form/Input">
sergey
parents: 296
diff changeset
138 [% FOREACH item IN model %]
sergey
parents: 296
diff changeset
139 <div class="itemContainer">
sergey
parents: 296
diff changeset
140 [% Display(item) %]
sergey
parents: 296
diff changeset
141 </div>
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
142 [% END %]
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
143 </div>
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
144
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
145 =end text
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
146
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
147 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
148
299
sergey
parents: 296
diff changeset
149 Легкая обертка вокруг шаблона, позволяет изолировать пространство имен шаблона,
sergey
parents: 296
diff changeset
150 а также реализовать собственные методы по представлению данных (в случае если
sergey
parents: 296
diff changeset
151 это проще сделать в виде методов класса).
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
152
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
153 =head2 BLOCKS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
154
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
155 =head3 META
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
156
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
157 Атрибуты C<META> C<layout>, C<title> будут перенесены в свойства элемента
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
158 управления.
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
159
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
160 =head3 INIT
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
161
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
162 Данный блок шаблона управления выполняется один раз при создании первого
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
163 экземпляра элемента управления, в пространстве имен документа. Может
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
164 использоваться для формирования заголовочной части документа, скрипта
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
165 подключающего C<js> модули и т.п.
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
166
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
167 Выполнение данного блока производится фабрикой элементов управления.
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
168
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
169 =head2 TEMPLATE VARS
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
170
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
171 Каждый шаблон имеет собственное пространство имен, вложенное в пространство имен
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
172 фабрики элементов (которая разделяет пространство имен документа). В шаблоне
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
173 могут определяться новые переменные, однако они останутся локальными для блоков.
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
174
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
175 Чтобы передать данные между блоками следует использовать ссылку на элемент
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
176 управления C<this>.
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
177
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
178 =begin text
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
179
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
180 [%
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
181 BLOCK CTOR;
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
182 this.extraCssClass = 'active';
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
183 text = "this text will gone";
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
184 END;
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
185 %]
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
186
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
187 <div class="$this.extraCssClass">some text $text</div>
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
188
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
189 =end text
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
190
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
191 В примере выше переменная C<$text> установленная в конструкторе шаблона, при
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
192 отображении элемента управления будет неопределена.
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
193
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
194 =over
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
195
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
196 =item * C<this>
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
197
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
198 ссылка на объект элемента управления
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
199
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
200 =item * C<component>
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
201
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
202 ссылка на текущий шаблон, устанавливается автоматически в методе
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
203 C<Template::Context::process>.
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
204
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
205 =item * C<template>
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
206
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
207 ссылка на шаблон элемента управления, для совместимости с C<TT>
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
208
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
209 =back
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
210
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
211 =head1 MEMBERS
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
212
300
cin
parents: 299
diff changeset
213 =head2 C<[get]context>
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
214
300
cin
parents: 299
diff changeset
215 Контекст элемента управления, хранит пременные шаблона. Фабрика элементов
cin
parents: 299
diff changeset
216 управления создает новый контекст пространство имен которого вложено в
cin
parents: 299
diff changeset
217 пространство имен документа.
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
218
300
cin
parents: 299
diff changeset
219 Контекст следует использовать только при рендеринге документа.
cin
parents: 299
diff changeset
220
cin
parents: 299
diff changeset
221 =head2 C<[get,set]template>
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
222
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
223 C<Template::Document> Шаблон элемента управления.
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
224
300
cin
parents: 299
diff changeset
225 =head2 C<AUTOLOAD>
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
226
265
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
227 Для удобства работы с шаблоном, элементы управления предоставляю доступ к своим
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
228 свойствам через метод C<AUTOLOAD>. Имена свойств должны начинаться со строчной
6b6d4b2275a1 improved documentation
cin
parents: 241
diff changeset
229 буквы.
187
927653d01f4f TTControl::AUTOLOAD now accesses nodeProperties
sergey
parents: 186
diff changeset
230
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
231 =cut