Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTControl.pm @ 253:0a228a35645c
added application exception class
author | sergey |
---|---|
date | Mon, 19 Nov 2012 17:54:01 +0400 |
parents | f48a1a9f4fa2 |
children | 6b6d4b2275a1 |
rev | line source |
---|---|
181 | 1 package IMPL::Web::View::TTControl; |
2 use strict; | |
3 | |
234 | 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); |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
6 use Scalar::Util qw(blessed); |
234 | 7 use IMPL::declare { |
8 require => { | |
9 TTContext => 'Template::Context', | |
10 Exception => 'IMPL::Exception', | |
238 | 11 ArgumentException => '-IMPL::InvalidArgumentException', |
12 OperationException => '-IMPL::InvalidOperationException' | |
234 | 13 }, |
14 base => [ | |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
15 'IMPL::Object' => undef |
234 | 16 ], |
17 props => [ | |
18 id => PROP_RO, | |
238 | 19 attributes => PROP_RW, |
20 name => PROP_RO, | |
234 | 21 context => PROP_RO, |
22 template => PROP_RO | |
23 ] | |
24 }; | |
25 | |
181 | 26 |
187 | 27 { |
194 | 28 my $nextId = 1; |
29 sub _GetNextId { | |
30 return $nextId++; | |
31 } | |
187 | 32 } |
181 | 33 |
238 | 34 our $AutoloadRegex = qr/^[a-z]/; |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
35 our @REFLECT_META = qw(title layout); |
238 | 36 |
181 | 37 sub CTOR { |
194 | 38 my ($this,$name,$template,$context,$refProps) = @_; |
39 | |
40 $name ||= "control"; | |
41 | |
42 $this->template( $template ) or die new IMPL::ArgumentException("A template is required"); | |
43 $this->context( $context ) or die new IMPL::ArgumentException("A context is required"); | |
44 | |
45 $this->id($name . "-" . _GetNextId()) unless $this->id; | |
46 | |
238 | 47 $this->name($name); |
48 $this->attributes({}); | |
194 | 49 |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
50 my %attrs; |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
51 |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
52 foreach my $meta ( @REFLECT_META ) { |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
53 next if $meta =~ /^_/; |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
54 if( my $value = $template->$meta() ) { |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
55 $attrs{$meta} = $value; |
238 | 56 } |
57 } | |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
58 |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
59 hashApply(\%attrs,$refProps) if ref $refProps eq 'HASH'; |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
60 |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
61 while (my($key,$value) = each %attrs) { |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
62 $this->SetAttribute($key,$value); |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
63 } |
181 | 64 } |
65 | |
191 | 66 sub InitInstance { |
194 | 67 my ($this,$args) = @_; |
68 | |
69 $args ||= {}; | |
70 | |
71 if ( my $ctor = $this->template->blocks->{CTOR} ) { | |
212 | 72 $this->context->include($ctor, { %$args, this => $this, template => $this->template } ); |
194 | 73 } |
191 | 74 } |
75 | |
238 | 76 sub GetAttribute { |
77 my ($this,$name) = (shift,shift); | |
78 | |
79 if (my $method = $this->can($name)) { | |
80 unshift @_,$this; | |
81 goto &$method; | |
82 } else { | |
83 return $this->attributes->{$name}; | |
84 } | |
85 } | |
86 | |
87 sub SetAttribute { | |
88 my $this = shift; | |
89 my $name = shift; | |
90 | |
91 if (my $method = $this->can($name)) { | |
92 unshift @_, $this; | |
93 goto &$method; | |
94 } else { | |
95 return $this->attributes->{$name} = shift; | |
96 } | |
97 } | |
98 | |
234 | 99 sub GetRenderBlock { |
194 | 100 $_[0]->template->blocks->{RENDER} || $_[0]->template; |
187 | 101 } |
102 | |
103 sub Render { | |
194 | 104 my ($this,$args) = @_; |
105 | |
106 $args = {} unless ref $args eq 'HASH'; | |
107 | |
234 | 108 if(my $body = $this->GetRenderBlock ) { |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
109 return $this->context->include( $body, { %$args, this => $this, template => $this->template } ); |
194 | 110 } else { |
111 return ""; | |
112 } | |
185 | 113 } |
114 | |
115 sub AUTOLOAD { | |
194 | 116 our $AUTOLOAD; |
117 | |
118 my $method = ($AUTOLOAD =~ m/(\w+)$/)[0]; | |
119 | |
120 return if $method eq 'DESTROY'; | |
121 | |
238 | 122 if ($method =~ /$AutoloadRegex/) { |
123 my $this = shift; | |
241
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
124 |
f48a1a9f4fa2
+Added ViewResult to allow implementation of the view environment.
sergey
parents:
238
diff
changeset
|
125 die OperationException->new("can't invoke method '$method' on an unblessed reference") unless blessed $this; |
238 | 126 |
127 return @_ ? $this->SetAttribute($method,@_) : $this->GetAttribute($method); | |
128 } else { | |
129 die OperationException->new("The specified method '$method' doesn't exists"); | |
130 } | |
181 | 131 } |
132 | |
133 1; | |
134 | |
135 __END__ | |
136 | |
137 =pod | |
138 | |
139 =head1 NAME | |
140 | |
141 C<IMPL::Web::View::TTControl> | |
142 | |
143 =head1 SYNPOSIS | |
144 | |
145 =head1 DESCRIPTION | |
146 | |
147 =head2 BLOCKS | |
148 | |
149 =head3 INIT | |
150 | |
213 | 151 Данный блок шаблона управления выполняется один раз при создании первого экземпляра элемента управления, |
152 может использоваться для формирования заголовочной части документа, скрипта подключающего ajax модули | |
153 при необходимости и т.п. | |
181 | 154 |
155 =head3 CTOR | |
156 | |
157 данный блок выполняется каждый раз при создании нового экземпляра элемента управления, при этом переменная C<this> | |
213 | 158 указывает на эземпляр элемента упарвления. Данный блок можно использовать для инициализации свойств элемента |
159 управления. | |
181 | 160 |
161 =head3 RENDER | |
162 | |
213 | 163 Данный блок выполняется при вызове метода C<Render()>, вывод данного блока и есть результат отображения элемента управления. |
164 Если в шаблоне нет блока C<RENDER>, то сам шаблон считается таковым. | |
187 | 165 |
166 =head2 TEMPLATE VARS | |
167 | |
168 Каждый шаблон имеет собственное пространство имен, унаследованное от пространства имен фабрики элементов (которая в свою очередь наследует контекст документа). | |
169 В шаблоне могут определяться новые переменные, которые разделяются между блоками. Также доступны стандартные переменные | |
170 | |
171 =over | |
172 | |
173 =item * C<this> ссылка на объект элемента управления | |
174 | |
175 =item * C<component> ссылка на текущий шаблон, устанавливается автоматически в методе C<Template::Context::process>. | |
176 | |
177 =item * C<template> ссылка на шаблон элемента управления, для совместимости с C<TT> | |
178 | |
179 =back | |
180 | |
181 =head1 MEMBERS | |
182 | |
183 =over | |
184 | |
185 =item * C<[get]context> | |
186 | |
187 Контекст элемента управления, хранит пременные шаблона. Наследуется от контекста фабрики элементов управления, который | |
188 наследуется от контекста документа. | |
189 | |
190 =item * C<[get,set]template> | |
191 | |
192 C<Template::Document> Шаблон элемента управления. | |
193 | |
194 =item * C<AUTOLOAD> | |
195 | |
196 Для удобства работы с шаблоном, элементы управления предоставляю доступ к своим свойствам через метод C<AUTOLOAD>. | |
197 | |
198 =back | |
181 | 199 |
200 | |
201 C<lang ru> | |
202 | |
203 =cut |