annotate Lib/IMPL/Web/View/TTContext.pm @ 349:86b470004d47

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