annotate lib/IMPL/Web/View/TTContext.pm @ 417:3ed0c58e9da3 ref20150831

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