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',
|
|
16 Resources => 'IMPL::Resources'
|
348
|
17 },
|
|
18 base => [
|
343
|
19 'Template::Context' => '@_'
|
348
|
20 ]
|
343
|
21 };
|
|
22
|
347
|
23 BEGIN {
|
348
|
24 no strict 'refs';
|
347
|
25 foreach my $prop (qw(
|
348
|
26 root
|
347
|
27 base
|
|
28 tt_ext
|
348
|
29 tt_cache
|
347
|
30 parent
|
|
31 prefix
|
348
|
32 cache
|
|
33 includes
|
347
|
34 )) {
|
|
35 my $t = $prop;
|
|
36
|
348
|
37 *{__PACKAGE__ . '::' . $prop} = sub {
|
347
|
38 my $this = shift;
|
|
39 return @_ ? $this->stash->set($t, @_) : $this->stash->get($t);
|
|
40 }
|
|
41 }
|
|
42 }
|
|
43
|
343
|
44 sub clone {
|
|
45 my $this = shift;
|
347
|
46 my $params = shift;
|
343
|
47
|
|
48 $this->localise();
|
|
49
|
|
50 my $args = { %{$this} };
|
|
51
|
|
52 $this->delocalise();
|
|
53
|
348
|
54 my $class = ref($this);
|
343
|
55
|
|
56 delete $args->{CONFIG};
|
|
57
|
347
|
58 my $clone = $class->new($args);
|
|
59
|
|
60 $clone->stash->update($params) if $params;
|
|
61
|
|
62 return $clone;
|
345
|
63 }
|
|
64
|
|
65 sub find_template {
|
348
|
66 my ($this,$name) = @_;
|
|
67
|
|
68 my $cache = $this->tt_cache;
|
|
69 $this->tt_cache($cache = {}) unless $cache;
|
345
|
70
|
348
|
71 if(my $tpl = $cache->{$name}) {
|
|
72 return $tpl;
|
|
73 }
|
|
74
|
|
75 my @inc = ($this->base, @{$this->includes || []});
|
|
76
|
345
|
77 my $ext = $this->tt_ext || "";
|
348
|
78
|
|
79 my $file;
|
345
|
80
|
|
81 foreach my $dir (@inc) {
|
348
|
82 $file = $dir ? "$dir/$name" : $name;
|
|
83
|
|
84 my $base = join('/',splice([split(/\/+/,$file)],0,-1));
|
|
85
|
|
86 $file = $ext ? "$file.$ext" : $file;
|
|
87
|
|
88 warn "lookup: $file";
|
|
89
|
345
|
90 my $tt = eval { $this->template($file) };
|
|
91
|
348
|
92 return $cache->{$name} = {
|
|
93 base => $base,
|
349
|
94 labels => $this->load_labels($file),
|
348
|
95 template => $tt,
|
345
|
96 } if $tt;
|
|
97 }
|
|
98
|
|
99 $this->throw(Template::Constants::ERROR_FILE, "$name: not found");
|
|
100 }
|
|
101
|
347
|
102 sub display {
|
|
103 my $this = shift;
|
|
104 my $model = shift;
|
348
|
105 my ($template, $args);
|
347
|
106
|
|
107 if (ref $_[0] eq 'HASH') {
|
|
108 $args = shift;
|
|
109 } else {
|
|
110 $template = shift;
|
|
111 $args = shift;
|
|
112 }
|
|
113
|
348
|
114 my $prefix = $this->prefix;
|
|
115
|
349
|
116 warn "no resolve" if $args and $args->{_no_resolve};
|
|
117
|
|
118 if (not(($args and delete $args->{_no_resolve}) or ref $model)) {
|
348
|
119 $prefix = $prefix ? "${prefix}.${model}" : $model;
|
|
120 $model = $this->resolve_model($model);
|
|
121 } else {
|
|
122 $prefix = "";
|
|
123 }
|
|
124
|
|
125 $template = $template ? $this->find_template($template) : $this->find_template_for($model);
|
|
126
|
|
127 return $this->render(
|
|
128 $template,
|
|
129 hashApply(
|
|
130 {
|
|
131 prefix => $prefix,
|
|
132 model => $model,
|
|
133 },
|
|
134 $args
|
|
135 )
|
|
136 );
|
|
137 }
|
|
138
|
|
139 sub invoke_environment {
|
|
140 my ($this,$code,$env) = @_;
|
|
141
|
|
142 $env ||= {};
|
|
143
|
|
144 my $out = eval {
|
|
145 $this->localise(
|
|
146 hashApply(
|
|
147 {
|
|
148 root => $this->root || $this,
|
|
149 cache => TypeKeyedCollection->new(),
|
|
150 display => sub {
|
|
151 $this->display(@_);
|
|
152 },
|
|
153 render => sub {
|
|
154 $this->render(@_);
|
|
155 }
|
|
156 },
|
|
157 $env
|
|
158 )
|
|
159 );
|
|
160
|
|
161 &$code($this);
|
|
162 };
|
|
163
|
|
164 my $e = $@;
|
|
165 $this->delocalise();
|
|
166
|
|
167 die $e if $e;
|
|
168
|
|
169 return $out;
|
|
170 }
|
|
171
|
|
172 sub render {
|
|
173 my ($this,$template,$args) = @_;
|
|
174
|
|
175 $args ||= {};
|
|
176
|
|
177 #TODO handle classes
|
|
178
|
349
|
179 my ($base,$labels);
|
347
|
180
|
348
|
181 $template = $this->find_template($template) unless ref $template;
|
|
182
|
|
183 if (ref $template eq 'HASH') {
|
|
184 $base = $template->{base};
|
349
|
185 $labels = $template->{labels};
|
348
|
186 $template = $template->{template};
|
|
187 } else {
|
|
188 carp "got an invalid template object: $template";
|
|
189 $base = $this->base;
|
|
190 }
|
|
191
|
|
192 return $this->invoke_environment(
|
|
193 sub {
|
|
194 return shift->include($template,$args);
|
|
195 },
|
349
|
196 hashMerge(
|
|
197 $labels || {},
|
|
198 {
|
|
199 base => $base,
|
|
200 parent => $this
|
|
201 }
|
|
202 )
|
348
|
203 )
|
|
204 }
|
|
205
|
|
206 sub resolve_model {
|
|
207 my ($this,$prefix) = @_;
|
|
208
|
|
209 die ArgException->new(prefix => "the prefix must be specified")
|
|
210 unless defined $prefix;
|
|
211
|
|
212 #TODO handle DOM models
|
|
213
|
|
214 my @comp = map { $_, 0 } grep length($_), split(/\.|\[(\d+)\]/, $prefix);
|
347
|
215
|
348
|
216 return $this->stash->get(['model',0,@comp]);
|
|
217 }
|
|
218
|
|
219 sub find_template_for {
|
|
220 my ($this,$model) = @_;
|
347
|
221
|
348
|
222 my $type = typeof($model);
|
347
|
223
|
348
|
224 return $this->find_template('templates/plain') unless $type;
|
|
225
|
|
226 if (my $template = $this->cache->Get($type)) {
|
|
227 return $template;
|
|
228 } else {
|
|
229
|
|
230 no strict 'refs';
|
|
231
|
|
232 my @isa = $type;
|
|
233
|
|
234 while (@isa) {
|
|
235 my $sclass = shift @isa;
|
|
236
|
|
237 (my $name = $sclass) =~ s/:+/_/g;
|
|
238
|
|
239 $template = $this->find_template("templates/$name");
|
|
240
|
|
241 if ($template) {
|
|
242 $this->cache->Set($sclass,$template);
|
|
243 return $template;
|
|
244 }
|
|
245
|
|
246 push @isa, @{"${sclass}::ISA"};
|
|
247 }
|
|
248
|
|
249 }
|
|
250
|
|
251 return;
|
347
|
252 }
|
|
253
|
349
|
254 sub get_real_file {
|
|
255 my ($this,$fname) = @_;
|
|
256
|
|
257 my @path = split(/\/+/,$fname);
|
|
258
|
|
259 foreach my $provider (@{$this->load_templates || []}) {
|
|
260 foreach my $dir (@{$provider->paths || []}) {
|
|
261 my $realName = File::Spec->catfile($dir,@path);
|
|
262 return $realName if -f $realName;
|
|
263 }
|
|
264 }
|
|
265 }
|
|
266
|
|
267 sub load_labels {
|
|
268 my ($this,$fname) = @_;
|
|
269
|
|
270 $fname = $this->get_real_file($fname);
|
|
271
|
|
272 my %vars;
|
|
273
|
|
274 my $flabels = "$fname.labels";
|
|
275
|
|
276 if (-f $flabels) {
|
|
277
|
|
278 my %labels;
|
|
279 $labels{default} = IMPL::Resources::Strings::ParseStringsMap($flabels);
|
|
280
|
|
281 while(my($label,$text) = each %{$labels{default}}) {
|
|
282 $vars{$label} = sub {
|
|
283 my ($params) = @_;
|
|
284 my $locale = Resources->currentLocale;
|
|
285
|
|
286 unless ($labels{$locale}) {
|
|
287 $labels{$locale} = -f "$fname.$locale" ?
|
|
288 IMPL::Resources::Strings::ParseStringsMap("$fname.$locale") :
|
|
289 {};
|
|
290 }
|
|
291
|
|
292 return FormatMessage(($labels{$locale}{$label} || $text),$params);
|
|
293 }
|
|
294 }
|
|
295 }
|
|
296
|
|
297 return \%vars;
|
|
298 }
|
|
299
|
345
|
300 1;
|
|
301
|
|
302 __END__
|
|
303
|
|
304 =pod
|
|
305
|
|
306 =head1 NAME
|
|
307
|
|
308 C<IMPL::Web::View::TTContext> - доработанная версия контекста
|
|
309
|
|
310 =head1 DESCRIPTION
|
|
311
|
|
312 Расширяет функции C<Template::Context>
|
|
313
|
|
314 =begin plantuml
|
|
315
|
|
316 @startuml
|
|
317
|
348
|
318 object RootContext {
|
345
|
319 document
|
346
|
320 globals
|
345
|
321 }
|
|
322
|
|
323 object DocumentContext {
|
346
|
324 base
|
|
325 extends
|
|
326 }
|
|
327
|
|
328 object ControlContext {
|
|
329 base
|
|
330 extends
|
345
|
331 }
|
|
332
|
348
|
333 RootContext o-- DocumentContext
|
|
334 RootContext o-- ControlContext
|
345
|
335
|
346
|
336 Document -- DocumentContext
|
|
337 Control - ControlContext
|
345
|
338
|
348
|
339 Loader . RootContext: <<creates>>
|
346
|
340 Loader . Document: <<creates>>
|
|
341 Loader -up- Registry
|
345
|
342
|
|
343 @enduml
|
|
344
|
|
345 =end plantuml
|
|
346
|
|
347 =head1 MEMBERS
|
|
348
|
|
349 =head2 C<[get,set]base>
|
|
350
|
|
351 Префикс пути для поиска шаблонов
|
|
352
|
|
353 =head2 C<template($name)>
|
|
354
|
|
355 Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса.
|
|
356
|
|
357 =head2 C<clone()>
|
|
358
|
|
359 Создает копию контекста, при этом C<stash> локализуется, таким образом
|
|
360 клонированный контекст имеет собственное пространство имен, вложенное в
|
|
361 пространство родительского контекста.
|
|
362
|
|
363 =cut |