annotate Lib/IMPL/Web/View/TTContext.pm @ 356:97131d500f16

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