Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTContext.pm @ 380:1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
author | cin |
---|---|
date | Fri, 17 Jan 2014 15:58:57 +0400 |
parents | d5c8b955bf8d |
children | ced5937ff21a |
rev | line source |
---|---|
343 | 1 package IMPL::Web::View::TTContext; |
2 use strict; | |
3 use Template::Base; | |
348 | 4 use Carp qw(carp); |
349 | 5 use File::Spec(); |
6 use IMPL::Resources::Format qw(FormatMessage); | |
7 use IMPL::Resources::Strings(); | |
343 | 8 |
348 | 9 use IMPL::Exception(); |
349 | 10 use IMPL::lang qw(is typeof hashApply hashMerge); |
343 | 11 use IMPL::declare { |
348 | 12 require => { |
13 Document => '-Template::Document', | |
14 TypeKeyedCollection => 'IMPL::TypeKeyedCollection', | |
349 | 15 ArgException => '-IMPL::InvalidArgumentException', |
352 | 16 Resources => 'IMPL::Resources', |
371 | 17 Loader => 'IMPL::Code::Loader', |
18 MetadataBase => '-IMPL::Web::View::Metadata::BaseMeta', | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
19 Metadata => 'IMPL::Web::View::Metadata::ObjectMeta', |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
20 StringMap => 'IMPL::Resources::StringLocaleMap' |
348 | 21 }, |
22 base => [ | |
343 | 23 'Template::Context' => '@_' |
348 | 24 ] |
343 | 25 }; |
26 | |
347 | 27 BEGIN { |
348 | 28 no strict 'refs'; |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
29 # modules is a global (for the whole document) templates cache |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
30 # tt_cache is a local (for the current context only) templtes cache |
347 | 31 foreach my $prop (qw( |
348 | 32 root |
347 | 33 base |
34 tt_ext | |
348 | 35 tt_cache |
347 | 36 parent |
37 prefix | |
348 | 38 cache |
39 includes | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
40 modules |
352 | 41 aliases |
356 | 42 id |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
43 metadata |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
44 model |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
45 templateInfo |
347 | 46 )) { |
47 my $t = $prop; | |
48 | |
348 | 49 *{__PACKAGE__ . '::' . $prop} = sub { |
347 | 50 my $this = shift; |
51 return @_ ? $this->stash->set($t, @_) : $this->stash->get($t); | |
52 } | |
53 } | |
54 } | |
55 | |
343 | 56 sub clone { |
57 my $this = shift; | |
347 | 58 my $params = shift; |
343 | 59 |
60 $this->localise(); | |
61 | |
62 my $args = { %{$this} }; | |
63 | |
64 $this->delocalise(); | |
65 | |
348 | 66 my $class = ref($this); |
343 | 67 |
68 delete $args->{CONFIG}; | |
69 | |
347 | 70 my $clone = $class->new($args); |
71 | |
72 $clone->stash->update($params) if $params; | |
73 | |
74 return $clone; | |
345 | 75 } |
76 | |
356 | 77 sub get_next_id { |
78 my ($this) = @_; | |
79 | |
80 my $id = $this->stash->get('document.nextId') || 0; | |
81 $this->stash->set('document.nextId', $id + 1); | |
82 return "w-$id"; | |
83 } | |
84 | |
345 | 85 sub find_template { |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
86 my ($this,$name, $nothrow) = @_; |
348 | 87 |
88 my $cache = $this->tt_cache; | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
89 |
348 | 90 $this->tt_cache($cache = {}) unless $cache; |
345 | 91 |
348 | 92 if(my $tpl = $cache->{$name}) { |
93 return $tpl; | |
94 } | |
95 | |
96 my @inc = ($this->base, @{$this->includes || []}); | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
97 #my @inc = @{$this->includes || []}; |
348 | 98 |
345 | 99 my $ext = $this->tt_ext || ""; |
348 | 100 |
101 my $file; | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
102 |
345 | 103 foreach my $dir (@inc) { |
348 | 104 $file = $dir ? "$dir/$name" : $name; |
105 | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
106 my @parts = split(/\/+/,$file); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
107 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
108 my $templateName = pop @parts; |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
109 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
110 my $base = join('/',@parts); |
348 | 111 |
112 $file = $ext ? "$file.$ext" : $file; | |
113 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
114 if (exists($this->modules->{$file})) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
115 my $info = $this->modules->{$file}; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
116 return $cache->{$name} = $info |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
117 if $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
118 } else { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
119 if( my $tt = eval { $this->template($file) } ) { |
352 | 120 my $class; |
121 if ($class = $tt->class) { | |
122 $class = $this->aliases->{$class} || $class; | |
123 Loader->safe->Require($class); | |
124 } | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
125 my $info = { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
126 base => $base, |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
127 name => $templateName, |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
128 template => $tt, |
352 | 129 initialized => 0, |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
130 class => $class, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
131 file => $file |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
132 }; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
133 $this->modules->{$file} = $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
134 return $cache->{$name} = $info; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
135 } else { |
353 | 136 my $err = $@; |
137 | |
138 for(my $t = $err; is($t,'Template::Exception'); $t = $t->info ) { | |
139 die $err unless $t->type eq Template::Constants::ERROR_FILE; | |
140 } | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
141 $this->modules->{$file} = undef; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
142 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
143 } |
345 | 144 } |
145 | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
146 $this->throw(Template::Constants::ERROR_FILE, "$name: not found") |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
147 unless $nothrow; |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
148 return; |
345 | 149 } |
150 | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
151 sub display_for { |
347 | 152 my $this = shift; |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
153 my $path = shift; |
348 | 154 my ($template, $args); |
347 | 155 |
156 if (ref $_[0] eq 'HASH') { | |
157 $args = shift; | |
158 } else { | |
159 $template = shift; | |
160 $args = shift; | |
161 } | |
162 | |
348 | 163 my $prefix = $this->prefix; |
164 | |
354 | 165 my $info; |
371 | 166 my $meta = $this->resolve_model($path,$args) |
167 or return "[not found '$path']"; | |
354 | 168 |
371 | 169 $info->{prefix} = join('.', grep($_, $prefix, $path)); |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
170 $info->{model} = $meta->model; |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
171 $info->{metadata} = $meta; |
348 | 172 |
355
8dfb9df07d02
added support for the custom resolvers for the TTView
sergey
parents:
354
diff
changeset
|
173 $template ||= $info->{template}; |
371 | 174 $template = $template ? $this->find_template($template) : $this->find_template_for($info->{metadata}); |
348 | 175 |
176 return $this->render( | |
177 $template, | |
178 hashApply( | |
354 | 179 $info, |
348 | 180 $args |
181 ) | |
182 ); | |
183 } | |
184 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
185 sub display_model { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
186 my $this = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
187 my $model = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
188 my ($template, $args); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
189 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
190 if (ref $_[0] eq 'HASH') { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
191 $args = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
192 } else { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
193 $template = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
194 $args = shift; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
195 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
196 |
371 | 197 #copy |
198 $args = { %{$args || {}} }; | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
199 |
371 | 200 $args->{prefix} = join('.',grep($_,$this->prefix,$args->{path})) |
201 unless defined $args->{prefix}; | |
202 | |
203 if (is($model,MetadataBase)) { | |
204 $args->{model} = $model->model; | |
205 $args->{metadata} = $model; | |
206 } else { | |
207 $args->{model} = $model; | |
208 $args->{metadata} = Metadata->GetMetadataForModel($model); | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
209 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
210 |
371 | 211 $template = $template ? $this->find_template($template) : $this->find_template_for($args->{metadata}); |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
212 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
213 return $this->render( |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
214 $template, |
371 | 215 $args |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
216 ); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
217 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
218 |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
219 # обеспечивает необходимый уровень изоляции между контекстами |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
220 # $code - код, который нужно выполнить в новом контексте |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
221 # $env - хеш с переменными, которые будут переданы в новый контекст |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
222 # в процессе будет создан клон корневого контекста, со всеми его свойствами |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
223 # затем новый контекст будет локализован и в него будут добавлены новые переменные из $env |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
224 # созданный контекст будет передан параметром в $code |
348 | 225 sub invoke_environment { |
226 my ($this,$code,$env) = @_; | |
227 | |
228 $env ||= {}; | |
229 | |
354 | 230 my $ctx = ($this->root || $this)->clone(); |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
231 |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
232 my @includes = @{$this->includes || []}; |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
233 |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
234 if ($this->base) { |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
235 unshift @includes, $this->base; |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
236 } |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
237 |
348 | 238 my $out = eval { |
354 | 239 $ctx->localise( |
348 | 240 hashApply( |
241 { | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
242 includes => \@includes, |
352 | 243 aliases => $this->aliases || {}, |
354 | 244 root => $this->root || $ctx, |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
245 modules => $this->modules || {}, |
348 | 246 cache => TypeKeyedCollection->new(), |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
247 display_for => sub { |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
248 $ctx->display_for(@_); |
348 | 249 }, |
250 render => sub { | |
354 | 251 $ctx->render(@_); |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
252 }, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
253 display_model => sub { |
354 | 254 $ctx->display_model(@_); |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
255 }, |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
256 tt_cache => {}, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
257 labels => sub { |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
258 $ctx->load_labels(@_); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
259 } |
348 | 260 }, |
261 $env | |
262 ) | |
263 ); | |
264 | |
354 | 265 &$code($ctx); |
348 | 266 }; |
267 | |
268 my $e = $@; | |
354 | 269 $ctx->delocalise(); |
348 | 270 |
271 die $e if $e; | |
272 | |
273 return $out; | |
274 } | |
275 | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
276 # использует указанный шаблон для создания фрагмента документа |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
277 # шаблон может быть как именем, так и хешем, содержащим информацию |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
278 # о шаблоне. |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
279 # отдельно следует отметить, что данный метод создает новый контекст |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
280 # для выполнения шаблона в котором задает переменные base, parent, id |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
281 # а также создает переменные для строковых констант из labels |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
282 # хеш с переменными $args будет передан самому шаблону в момент выполнения |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
283 # если у шаблона указан класс элемента управления, то при выполнении шаблона |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
284 # будет создан экземпляр этого класса и процесс выполнения шаблона будет |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
285 # делегирован методу Render этого экземпляра. |
348 | 286 sub render { |
287 my ($this,$template,$args) = @_; | |
288 | |
289 $args ||= {}; | |
290 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
291 my $info = ref $template ? $template : $this->find_template($template); |
348 | 292 |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
293 if (ref($info) ne 'HASH') { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
294 carp "got an invalid template object: $info (" . ref($info) . ")"; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
295 $info = { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
296 template => $info, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
297 base => $this->base, |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
298 initialized => 1 |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
299 }; |
348 | 300 } |
301 | |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
302 return $this->invoke_environment( |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
303 sub { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
304 my $ctx = shift; |
352 | 305 |
351
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
306 unless($info->{initialized}) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
307 if(my $init = $info->{template}->blocks->{INIT}) { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
308 $info->{initialized} = 1; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
309 eval { |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
310 $ctx->visit($info->{template}->blocks); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
311 $ctx->include($init); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
312 }; |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
313 $ctx->leave(); |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
314 } |
cfd7570c2af2
working on TTView: created TTView class for rendering models
cin
parents:
349
diff
changeset
|
315 } |
352 | 316 |
317 if (my $class = $info->{class}) { | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
318 $class->new($ctx,$info->{template},$args)->Render({}); |
352 | 319 } else { |
320 return $ctx->include($info->{template},$args); | |
321 } | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
322 }, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
323 { |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
324 base => $info->{base}, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
325 parent => $this, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
326 id => $this->get_next_id, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
327 templateInfo => $info |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
328 } |
348 | 329 ) |
330 } | |
331 | |
332 sub resolve_model { | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
333 my ($this,$prefix) = @_; |
348 | 334 |
335 die ArgException->new(prefix => "the prefix must be specified") | |
336 unless defined $prefix; | |
337 | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
338 my $meta = $this->metadata; |
371 | 339 unless($meta) { |
340 $meta = Metadata->GetMetadataForModel($this->model); | |
341 $this->metadata($meta); | |
342 } | |
343 | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
344 foreach my $part (grep length($_), split(/\.|\[(\d+)\]/, $prefix)) { |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
345 last unless $meta; |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
346 if ($part =~ /^\d+$/) { |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
347 $meta = $meta->GetItem($part); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
348 } else { |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
349 $meta = $meta->GetProperty($part); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
350 } |
354 | 351 } |
352 | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
353 return $meta; |
348 | 354 } |
355 | |
356 sub find_template_for { | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
357 my ($this,$meta, $nothrow) = @_; |
347 | 358 |
371 | 359 die ArgException->new(meta => 'An invalid metadata is supplied') |
360 unless is($meta,MetadataBase); | |
361 | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
362 return $this->find_template($meta->template) |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
363 if ($meta->template); |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
364 |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
365 my $type = $meta->modelType; |
347 | 366 |
348 | 367 return $this->find_template('templates/plain') unless $type; |
368 | |
369 if (my $template = $this->cache->Get($type)) { | |
370 return $template; | |
371 } else { | |
372 | |
373 no strict 'refs'; | |
374 | |
375 my @isa = $type; | |
376 | |
377 while (@isa) { | |
378 my $sclass = shift @isa; | |
379 | |
380 (my $name = $sclass) =~ s/:+/_/g; | |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
381 my ($shortName) = ($sclass =~ m/(\w+)$/); |
348 | 382 |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
383 $template = $this->find_template("templates/$name",1) || $this->find_template("templates/$shortName",1); |
348 | 384 |
385 if ($template) { | |
386 $this->cache->Set($sclass,$template); | |
387 return $template; | |
388 } | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
389 |
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
390 #todo $meta->GetISA |
348 | 391 push @isa, @{"${sclass}::ISA"}; |
392 } | |
393 | |
394 } | |
370
cbf4febf0930
ObjectMeta, Tests, migrating to the new metadata model.
sergey
parents:
359
diff
changeset
|
395 $this->throw(Template::Constants::ERROR_FILE, "can't find a template for the model $type") |
359
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
396 unless $nothrow; |
833e663796c4
TTView: added view variable to pass rendering context between controls
sergey
parents:
356
diff
changeset
|
397 |
348 | 398 return; |
347 | 399 } |
400 | |
349 | 401 sub get_real_file { |
402 my ($this,$fname) = @_; | |
403 | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
404 return unless length $fname; |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
405 |
349 | 406 my @path = split(/\/+/,$fname); |
407 | |
408 foreach my $provider (@{$this->load_templates || []}) { | |
409 foreach my $dir (@{$provider->paths || []}) { | |
410 my $realName = File::Spec->catfile($dir,@path); | |
411 return $realName if -f $realName; | |
412 } | |
413 } | |
414 } | |
415 | |
416 sub load_labels { | |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
417 my ($this,$data) = @_; |
349 | 418 |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
419 die ArgException->new("A hash reference is required") |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
420 unless ref($data) eq 'HASH'; |
349 | 421 |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
422 my $stringMap = StringMap->new($data); |
349 | 423 |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
424 $this->stash->update({ |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
425 map { |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
426 my $id = $_; |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
427 $id, |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
428 sub { |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
429 $stringMap->GetString($id,@_); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
430 }; |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
431 } keys %$data |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
432 }); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
433 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
434 my $ti = $this->templateInfo || {}; |
349 | 435 |
380
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
436 if (my $fullName = $this->get_real_file($ti->{file})) { |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
437 my ($vol,$dir,$fname) = File::Spec->splitpath($fullName); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
438 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
439 my $name = $this->templateInfo->{name}; |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
440 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
441 my $localePath = File::Spec->catpath($vol, File::Spec->catdir($dir,'locale'),''); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
442 |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
443 $stringMap->name($name); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
444 $stringMap->paths($localePath); |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
445 } |
1eca08048ba9
TTContext migrated to the unified localization mechanism IMPL::Resources::StringLocaleMap
cin
parents:
371
diff
changeset
|
446 return; |
349 | 447 } |
448 | |
345 | 449 1; |
450 | |
451 __END__ | |
452 | |
453 =pod | |
454 | |
455 =head1 NAME | |
456 | |
457 C<IMPL::Web::View::TTContext> - доработанная версия контекста | |
458 | |
459 =head1 DESCRIPTION | |
460 | |
461 Расширяет функции C<Template::Context> | |
462 | |
463 =begin plantuml | |
464 | |
465 @startuml | |
466 | |
348 | 467 object RootContext { |
345 | 468 document |
346 | 469 globals |
345 | 470 } |
471 | |
472 object DocumentContext { | |
346 | 473 base |
474 extends | |
475 } | |
476 | |
477 object ControlContext { | |
478 base | |
479 extends | |
345 | 480 } |
481 | |
348 | 482 RootContext o-- DocumentContext |
483 RootContext o-- ControlContext | |
345 | 484 |
346 | 485 Document -- DocumentContext |
486 Control - ControlContext | |
345 | 487 |
348 | 488 Loader . RootContext: <<creates>> |
346 | 489 Loader . Document: <<creates>> |
490 Loader -up- Registry | |
345 | 491 |
492 @enduml | |
493 | |
494 =end plantuml | |
495 | |
496 =head1 MEMBERS | |
497 | |
498 =head2 C<[get,set]base> | |
499 | |
500 Префикс пути для поиска шаблонов | |
501 | |
502 =head2 C<template($name)> | |
503 | |
504 Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса. | |
505 | |
506 =head2 C<clone()> | |
507 | |
508 Создает копию контекста, при этом C<stash> локализуется, таким образом | |
509 клонированный контекст имеет собственное пространство имен, вложенное в | |
510 пространство родительского контекста. | |
511 | |
512 =cut |