annotate Lib/IMPL/Web/View/TTContext.pm @ 351:cfd7570c2af2

working on TTView: created TTView class for rendering models
author cin
date Tue, 08 Oct 2013 17:40:35 +0400
parents 86b470004d47
children 675cd1829255
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
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
34 modules
347
cin
parents: 346
diff changeset
35 )) {
cin
parents: 346
diff changeset
36 my $t = $prop;
cin
parents: 346
diff changeset
37
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
38 *{__PACKAGE__ . '::' . $prop} = sub {
347
cin
parents: 346
diff changeset
39 my $this = shift;
cin
parents: 346
diff changeset
40 return @_ ? $this->stash->set($t, @_) : $this->stash->get($t);
cin
parents: 346
diff changeset
41 }
cin
parents: 346
diff changeset
42 }
cin
parents: 346
diff changeset
43 }
cin
parents: 346
diff changeset
44
343
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
45 sub clone {
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
46 my $this = shift;
347
cin
parents: 346
diff changeset
47 my $params = shift;
343
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
48
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
49 $this->localise();
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
50
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
51 my $args = { %{$this} };
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
52
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
53 $this->delocalise();
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
54
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
55 my $class = ref($this);
343
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
56
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
57 delete $args->{CONFIG};
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
58
347
cin
parents: 346
diff changeset
59 my $clone = $class->new($args);
cin
parents: 346
diff changeset
60
cin
parents: 346
diff changeset
61 $clone->stash->update($params) if $params;
cin
parents: 346
diff changeset
62
cin
parents: 346
diff changeset
63 return $clone;
345
cin
parents: 343
diff changeset
64 }
cin
parents: 343
diff changeset
65
cin
parents: 343
diff changeset
66 sub find_template {
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
67 my ($this,$name) = @_;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
68
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
69 my $cache = $this->tt_cache;
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
70
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
71 $this->tt_cache($cache = {}) unless $cache;
345
cin
parents: 343
diff changeset
72
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
73 if(my $tpl = $cache->{$name}) {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
74 return $tpl;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
75 }
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
76
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
77 my @inc = ($this->base, @{$this->includes || []});
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
78
345
cin
parents: 343
diff changeset
79 my $ext = $this->tt_ext || "";
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
80
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
81 my $file;
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
82
345
cin
parents: 343
diff changeset
83 foreach my $dir (@inc) {
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
84 $file = $dir ? "$dir/$name" : $name;
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 my $base = join('/',splice([split(/\/+/,$file)],0,-1));
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 $file = $ext ? "$file.$ext" : $file;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
89
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
90 if (exists($this->modules->{$file})) {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
91 my $info = $this->modules->{$file};
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
92 return $cache->{$name} = $info
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
93 if $info;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
94 } else {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
95 if( my $tt = eval { $this->template($file) } ) {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
96 my $info = {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
97 base => $base,
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
98 labels => $this->load_labels($file),
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
99 template => $tt,
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
100 initialized => 0
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
101 };
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
102 $this->modules->{$file} = $info;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
103 return $cache->{$name} = $info;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
104 } else {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
105 $this->modules->{$file} = undef;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
106 }
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
107 }
345
cin
parents: 343
diff changeset
108 }
cin
parents: 343
diff changeset
109
cin
parents: 343
diff changeset
110 $this->throw(Template::Constants::ERROR_FILE, "$name: not found");
cin
parents: 343
diff changeset
111 }
cin
parents: 343
diff changeset
112
347
cin
parents: 346
diff changeset
113 sub display {
cin
parents: 346
diff changeset
114 my $this = shift;
cin
parents: 346
diff changeset
115 my $model = shift;
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
116 my ($template, $args);
347
cin
parents: 346
diff changeset
117
cin
parents: 346
diff changeset
118 if (ref $_[0] eq 'HASH') {
cin
parents: 346
diff changeset
119 $args = shift;
cin
parents: 346
diff changeset
120 } else {
cin
parents: 346
diff changeset
121 $template = shift;
cin
parents: 346
diff changeset
122 $args = shift;
cin
parents: 346
diff changeset
123 }
cin
parents: 346
diff changeset
124
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
125 my $prefix = $this->prefix;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
126
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
127 if (not(($args and delete $args->{_no_resolve}) or ref $model)) {
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
128 $prefix = $prefix ? "${prefix}.${model}" : $model;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
129 $model = $this->resolve_model($model);
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
130 } else {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
131 $prefix = "";
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
132 }
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 $template = $template ? $this->find_template($template) : $this->find_template_for($model);
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 return $this->render(
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
137 $template,
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
138 hashApply(
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
139 {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
140 prefix => $prefix,
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
141 model => $model,
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
142 },
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
143 $args
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
144 )
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
145 );
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
146 }
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
147
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
148 sub display_model {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
149 my $this = shift;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
150 my $model = shift;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
151 my ($template, $args);
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
152
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
153 if (ref $_[0] eq 'HASH') {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
154 $args = shift;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
155 } else {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
156 $template = shift;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
157 $args = shift;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
158 }
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
159
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
160 $args ||= {};
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
161
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
162 my $prefix = delete $args->{prefix} || $this->prefix;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
163
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
164 if (my $rel = delete $args->{rel}) {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
165 $prefix = $prefix ? "${prefix}.${rel}" : $rel;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
166 }
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
167
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
168 $template = $template ? $this->find_template($template) : $this->find_template_for($model);
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
169
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
170 return $this->render(
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
171 $template,
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
172 hashApply(
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
173 {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
174 prefix => $prefix,
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
175 model => $model,
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
176 },
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
177 $args
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
178 )
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
179 );
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
180 }
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
181
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
182 sub invoke_environment {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
183 my ($this,$code,$env) = @_;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
184
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
185 $env ||= {};
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
186
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
187 my $out = eval {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
188 $this->localise(
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
189 hashApply(
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 root => $this->root || $this,
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
192 modules => $this->modules || {},
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
193 cache => TypeKeyedCollection->new(),
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
194 display => sub {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
195 $this->display(@_);
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
196 },
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
197 render => sub {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
198 $this->render(@_);
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
199 },
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
200 display_model => sub {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
201 $this->display_model(@_);
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
202 },
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
203 tt_cache => {}
348
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 $env
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
206 )
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
207 );
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 &$code($this);
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
210 };
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 my $e = $@;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
213 $this->delocalise();
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
214
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
215 die $e if $e;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
216
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
217 return $out;
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
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
220 sub render {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
221 my ($this,$template,$args) = @_;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
222
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
223 $args ||= {};
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
224
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
225 my $info = ref $template ? $template : $this->find_template($template);
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
226
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
227 if (ref($info) ne 'HASH') {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
228 carp "got an invalid template object: $info (" . ref($info) . ")";
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
229 $info = {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
230 template => $info,
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
231 base => $this->base,
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
232 initialized => 1
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
233 };
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
234 }
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
235
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
236 return $this->invoke_environment(
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
237 sub {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
238 my $ctx = shift;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
239
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
240 unless($info->{initialized}) {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
241 if(my $init = $info->{template}->blocks->{INIT}) {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
242 $info->{initialized} = 1;
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
243 eval {
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
244 $ctx->visit($info->{template}->blocks);
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
245 $ctx->include($init);
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
246 };
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
247 $ctx->leave();
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
248 }
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
249 }
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
250
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
251 return $ctx->include($info->{template},$args);
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
252 },
349
86b470004d47 added lables loading
cin
parents: 348
diff changeset
253 hashMerge(
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
254 $info->{labels} || {},
349
86b470004d47 added lables loading
cin
parents: 348
diff changeset
255 {
351
cfd7570c2af2 working on TTView: created TTView class for rendering models
cin
parents: 349
diff changeset
256 base => $info->{base},
349
86b470004d47 added lables loading
cin
parents: 348
diff changeset
257 parent => $this
86b470004d47 added lables loading
cin
parents: 348
diff changeset
258 }
86b470004d47 added lables loading
cin
parents: 348
diff changeset
259 )
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
260 )
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
261 }
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
262
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
263 sub resolve_model {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
264 my ($this,$prefix) = @_;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
265
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
266 die ArgException->new(prefix => "the prefix must be specified")
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
267 unless defined $prefix;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
268
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
269 #TODO handle DOM models
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
270
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
271 my @comp = map { $_, 0 } grep length($_), split(/\.|\[(\d+)\]/, $prefix);
347
cin
parents: 346
diff changeset
272
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
273 return $this->stash->get(['model',0,@comp]);
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
274 }
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
275
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
276 sub find_template_for {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
277 my ($this,$model) = @_;
347
cin
parents: 346
diff changeset
278
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
279 my $type = typeof($model);
347
cin
parents: 346
diff changeset
280
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
281 return $this->find_template('templates/plain') unless $type;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
282
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
283 if (my $template = $this->cache->Get($type)) {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
284 return $template;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
285 } else {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
286
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
287 no strict 'refs';
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
288
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
289 my @isa = $type;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
290
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
291 while (@isa) {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
292 my $sclass = shift @isa;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
293
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
294 (my $name = $sclass) =~ s/:+/_/g;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
295
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
296 $template = $this->find_template("templates/$name");
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
297
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
298 if ($template) {
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
299 $this->cache->Set($sclass,$template);
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
300 return $template;
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
301 }
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
302
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
303 push @isa, @{"${sclass}::ISA"};
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
304 }
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
305
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
306 }
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
307
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
308 return;
347
cin
parents: 346
diff changeset
309 }
cin
parents: 346
diff changeset
310
349
86b470004d47 added lables loading
cin
parents: 348
diff changeset
311 sub get_real_file {
86b470004d47 added lables loading
cin
parents: 348
diff changeset
312 my ($this,$fname) = @_;
86b470004d47 added lables loading
cin
parents: 348
diff changeset
313
86b470004d47 added lables loading
cin
parents: 348
diff changeset
314 my @path = split(/\/+/,$fname);
86b470004d47 added lables loading
cin
parents: 348
diff changeset
315
86b470004d47 added lables loading
cin
parents: 348
diff changeset
316 foreach my $provider (@{$this->load_templates || []}) {
86b470004d47 added lables loading
cin
parents: 348
diff changeset
317 foreach my $dir (@{$provider->paths || []}) {
86b470004d47 added lables loading
cin
parents: 348
diff changeset
318 my $realName = File::Spec->catfile($dir,@path);
86b470004d47 added lables loading
cin
parents: 348
diff changeset
319 return $realName if -f $realName;
86b470004d47 added lables loading
cin
parents: 348
diff changeset
320 }
86b470004d47 added lables loading
cin
parents: 348
diff changeset
321 }
86b470004d47 added lables loading
cin
parents: 348
diff changeset
322 }
86b470004d47 added lables loading
cin
parents: 348
diff changeset
323
86b470004d47 added lables loading
cin
parents: 348
diff changeset
324 sub load_labels {
86b470004d47 added lables loading
cin
parents: 348
diff changeset
325 my ($this,$fname) = @_;
86b470004d47 added lables loading
cin
parents: 348
diff changeset
326
86b470004d47 added lables loading
cin
parents: 348
diff changeset
327 $fname = $this->get_real_file($fname);
86b470004d47 added lables loading
cin
parents: 348
diff changeset
328
86b470004d47 added lables loading
cin
parents: 348
diff changeset
329 my %vars;
86b470004d47 added lables loading
cin
parents: 348
diff changeset
330
86b470004d47 added lables loading
cin
parents: 348
diff changeset
331 my $flabels = "$fname.labels";
86b470004d47 added lables loading
cin
parents: 348
diff changeset
332
86b470004d47 added lables loading
cin
parents: 348
diff changeset
333 if (-f $flabels) {
86b470004d47 added lables loading
cin
parents: 348
diff changeset
334
86b470004d47 added lables loading
cin
parents: 348
diff changeset
335 my %labels;
86b470004d47 added lables loading
cin
parents: 348
diff changeset
336 $labels{default} = IMPL::Resources::Strings::ParseStringsMap($flabels);
86b470004d47 added lables loading
cin
parents: 348
diff changeset
337
86b470004d47 added lables loading
cin
parents: 348
diff changeset
338 while(my($label,$text) = each %{$labels{default}}) {
86b470004d47 added lables loading
cin
parents: 348
diff changeset
339 $vars{$label} = sub {
86b470004d47 added lables loading
cin
parents: 348
diff changeset
340 my ($params) = @_;
86b470004d47 added lables loading
cin
parents: 348
diff changeset
341 my $locale = Resources->currentLocale;
86b470004d47 added lables loading
cin
parents: 348
diff changeset
342
86b470004d47 added lables loading
cin
parents: 348
diff changeset
343 unless ($labels{$locale}) {
86b470004d47 added lables loading
cin
parents: 348
diff changeset
344 $labels{$locale} = -f "$fname.$locale" ?
86b470004d47 added lables loading
cin
parents: 348
diff changeset
345 IMPL::Resources::Strings::ParseStringsMap("$fname.$locale") :
86b470004d47 added lables loading
cin
parents: 348
diff changeset
346 {};
86b470004d47 added lables loading
cin
parents: 348
diff changeset
347 }
86b470004d47 added lables loading
cin
parents: 348
diff changeset
348
86b470004d47 added lables loading
cin
parents: 348
diff changeset
349 return FormatMessage(($labels{$locale}{$label} || $text),$params);
86b470004d47 added lables loading
cin
parents: 348
diff changeset
350 }
86b470004d47 added lables loading
cin
parents: 348
diff changeset
351 }
86b470004d47 added lables loading
cin
parents: 348
diff changeset
352 }
86b470004d47 added lables loading
cin
parents: 348
diff changeset
353
86b470004d47 added lables loading
cin
parents: 348
diff changeset
354 return \%vars;
86b470004d47 added lables loading
cin
parents: 348
diff changeset
355 }
86b470004d47 added lables loading
cin
parents: 348
diff changeset
356
345
cin
parents: 343
diff changeset
357 1;
cin
parents: 343
diff changeset
358
cin
parents: 343
diff changeset
359 __END__
cin
parents: 343
diff changeset
360
cin
parents: 343
diff changeset
361 =pod
cin
parents: 343
diff changeset
362
cin
parents: 343
diff changeset
363 =head1 NAME
cin
parents: 343
diff changeset
364
cin
parents: 343
diff changeset
365 C<IMPL::Web::View::TTContext> - доработанная версия контекста
cin
parents: 343
diff changeset
366
cin
parents: 343
diff changeset
367 =head1 DESCRIPTION
cin
parents: 343
diff changeset
368
cin
parents: 343
diff changeset
369 Расширяет функции C<Template::Context>
cin
parents: 343
diff changeset
370
cin
parents: 343
diff changeset
371 =begin plantuml
cin
parents: 343
diff changeset
372
cin
parents: 343
diff changeset
373 @startuml
cin
parents: 343
diff changeset
374
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
375 object RootContext {
345
cin
parents: 343
diff changeset
376 document
346
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
377 globals
345
cin
parents: 343
diff changeset
378 }
cin
parents: 343
diff changeset
379
cin
parents: 343
diff changeset
380 object DocumentContext {
346
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
381 base
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
382 extends
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
383 }
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
384
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
385 object ControlContext {
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
386 base
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
387 extends
345
cin
parents: 343
diff changeset
388 }
cin
parents: 343
diff changeset
389
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
390 RootContext o-- DocumentContext
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
391 RootContext o-- ControlContext
345
cin
parents: 343
diff changeset
392
346
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
393 Document -- DocumentContext
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
394 Control - ControlContext
345
cin
parents: 343
diff changeset
395
348
f116cd9fe7d9 working on TTView: pre-alpha version
cin
parents: 347
diff changeset
396 Loader . RootContext: <<creates>>
346
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
397 Loader . Document: <<creates>>
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
398 Loader -up- Registry
345
cin
parents: 343
diff changeset
399
cin
parents: 343
diff changeset
400 @enduml
cin
parents: 343
diff changeset
401
cin
parents: 343
diff changeset
402 =end plantuml
cin
parents: 343
diff changeset
403
cin
parents: 343
diff changeset
404 =head1 MEMBERS
cin
parents: 343
diff changeset
405
cin
parents: 343
diff changeset
406 =head2 C<[get,set]base>
cin
parents: 343
diff changeset
407
cin
parents: 343
diff changeset
408 Префикс пути для поиска шаблонов
cin
parents: 343
diff changeset
409
cin
parents: 343
diff changeset
410 =head2 C<template($name)>
cin
parents: 343
diff changeset
411
cin
parents: 343
diff changeset
412 Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса.
cin
parents: 343
diff changeset
413
cin
parents: 343
diff changeset
414 =head2 C<clone()>
cin
parents: 343
diff changeset
415
cin
parents: 343
diff changeset
416 Создает копию контекста, при этом C<stash> локализуется, таким образом
cin
parents: 343
diff changeset
417 клонированный контекст имеет собственное пространство имен, вложенное в
cin
parents: 343
diff changeset
418 пространство родительского контекста.
cin
parents: 343
diff changeset
419
cin
parents: 343
diff changeset
420 =cut