annotate Lib/IMPL/Web/View/TTLoader.pm @ 346:f05634287ac7

working on the view concept
author cin
date Mon, 30 Sep 2013 01:37:50 +0400
parents 72799d1211c5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
1 package IMPL::Web::View::TTLoader;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
2 use strict;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
3
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
4 use Template::Constants qw(:status);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
5
336
cin
parents: 311
diff changeset
6 use File::Spec();
265
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
7 use IMPL::Const qw(:prop);
345
cin
parents: 343
diff changeset
8 use Scalar::Util qw(weaken);
cin
parents: 343
diff changeset
9
265
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
10 use IMPL::declare {
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
11 require => {
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
12 Provider => 'Template::Provider',
343
9bdccdf1f50b Added a templates context
cin
parents: 336
diff changeset
13 Context => 'IMPL::Web::View::TTContext',
309
cin
parents: 308
diff changeset
14 TTRegistry => 'IMPL::Web::View::TTRegistry',
308
sergey
parents: 289
diff changeset
15 TTFactory => 'IMPL::Web::View::TTFactory',
309
cin
parents: 308
diff changeset
16 TTDocument => '-IMPL::Web::View::TTDocument',
265
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
17 Exception => 'IMPL::Exception',
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
18 ArgumentException => '-IMPL::InvalidArgumentException',
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
19 KeyNotFoundException => '-IMPL::KeyNotFoundException'
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
20 },
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
21 base => [
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
22 'IMPL::Object' => undef,
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
23 'IMPL::Object::Serializable' => undef
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
24 ],
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
25 props => [
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
26 provider => PROP_RO,
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
27 context => PROP_RO,
345
cin
parents: 343
diff changeset
28 registry => PROP_RO,
265
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
29 _globals => PROP_RW
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
30 ]
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
31 };
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
32
196
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
33 sub save {
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
34 my ($this,$context) = @_;
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
35
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
36 $context->AddVar($_, $this->$_()) for qw(options provider context ext layoutBase);
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
37 }
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
38
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
39 sub restore {
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
40 my ($class,$data,$surrogate) = @_;
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
41
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
42 my %params = @$data;
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
43
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
44 my $refOpts = delete $params{options};
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
45
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
46 if ($surrogate){
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
47 $surrogate->callCTOR($refOpts,%params);
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
48 } else {
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
49 $surrogate = $class->new($refOpts,%params);
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
50 }
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
51 return $surrogate;
196
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
52 }
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
53
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
54 sub CTOR {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
55 my ($this,$refOpts,%args) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
56
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
57 $refOpts ||= {};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
58
345
cin
parents: 343
diff changeset
59 # to aviod cyclic references we need to do a copy of $refOpts
cin
parents: 343
diff changeset
60 $refOpts->{LOAD_TEMPLATES} = Provider->new( { %$refOpts } );
cin
parents: 343
diff changeset
61
cin
parents: 343
diff changeset
62 my $ctx = Context->new( { %$refOpts } );
cin
parents: 343
diff changeset
63 $this->context($ctx);
cin
parents: 343
diff changeset
64
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
65 $this->_globals(ref $args{globals} eq 'HASH' ? $args{globals} : {});
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
66
345
cin
parents: 343
diff changeset
67 $ctx->tt_ext($args{ext} || '.tt');
cin
parents: 343
diff changeset
68
cin
parents: 343
diff changeset
69 $this->registry(TTRegitry->new($ctx));
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
70
345
cin
parents: 343
diff changeset
71 weaken($ctx);
cin
parents: 343
diff changeset
72 weaken($this);
cin
parents: 343
diff changeset
73 $ctx->stash->update({
cin
parents: 343
diff changeset
74 require => sub {
cin
parents: 343
diff changeset
75 my ($modname) = @_;
cin
parents: 343
diff changeset
76
cin
parents: 343
diff changeset
77 my @inc;
cin
parents: 343
diff changeset
78 push @inc, $ctx->base if $ctx->base;
cin
parents: 343
diff changeset
79
cin
parents: 343
diff changeset
80 my $ti = $ctx->find_template($name,@inc);
cin
parents: 343
diff changeset
81
cin
parents: 343
diff changeset
82 require $this->registry->Require($ti);
cin
parents: 343
diff changeset
83 },
cin
parents: 343
diff changeset
84 inclue => sub {
cin
parents: 343
diff changeset
85 my ($name) = @_;
cin
parents: 343
diff changeset
86
cin
parents: 343
diff changeset
87 my @inc;
cin
parents: 343
diff changeset
88 push @inc, $ctx->base if $ctx->base;
cin
parents: 343
diff changeset
89
cin
parents: 343
diff changeset
90 my $ti = $ctx->find_template($name,@inc);
cin
parents: 343
diff changeset
91
cin
parents: 343
diff changeset
92 return $ctx->include($ti->{template}, {base => $ti->{base}} );
cin
parents: 343
diff changeset
93 }
cin
parents: 343
diff changeset
94 });
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
95
345
cin
parents: 343
diff changeset
96
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
97 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
98
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
99 sub document {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
100 my ($this,$name,$vars) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
101
309
cin
parents: 308
diff changeset
102 $vars ||= {};
308
sergey
parents: 289
diff changeset
103
345
cin
parents: 343
diff changeset
104 my $factory = $this->registry->Require($name);
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
105
345
cin
parents: 343
diff changeset
106 return $factory->new(hashMerge($vars, $this->_globals));
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
107 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
108
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
109 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
110
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
111 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
112
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
113 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
114
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
115 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
116
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
117 C<IMPL::Web::View::TTLoader> - предоставляет глобальный контекст для загрузки шаблонов
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
118
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
119 =head1 SYNOPSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
120
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
121 =begin code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
122
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
123 use IMPL::Web::View::TTLoader();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
124
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
125 my $loader = new IMPL::Web::View::TTLoader(
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
126 {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
127 INCLUDE_PATH => [
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
128 '/my/app/tt',
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
129 '/my/app/tt/lib'
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
130 ]
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
131 },
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
132 ext => '.tt',
345
cin
parents: 343
diff changeset
133 globals => {
cin
parents: 343
diff changeset
134 images => '//cdn.mysite.net/static/images'
cin
parents: 343
diff changeset
135 }
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
136
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
137 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
138
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
139 my $doc = $loader->document('index');
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
140
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
141 my $html = $doc->Render();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
142
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
143 =end code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
144
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
145 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
146
265
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
147 Провайдер для загрузки шаблонов и документов. Имеет собственное пространство
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
148 имен, контекст выполнения шаблонов. Контект загрузчика инициализируется
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
149 шаблоном, в котором определяются глобальные переменные, которые будут доступны
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
150 в документах, однако их изменения будут локализованы.
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
151
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
152 Инициализация контекста провайдера происходит при первой загрузке любого
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
153 документа.
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
154
182
sergey
parents: 181
diff changeset
155 =head1 MEMBERS
sergey
parents: 181
diff changeset
156
265
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
157 =head2 C<CTOR($options,%args)>
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
158
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
159 =over
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
160
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
161 =item * C<$options>
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
162
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
163 Параметры контекста загрузчика, контексты документов и элементов управления
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
164 также унаследуют эти свойства. Напрямую передаются в конструктор
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
165 C<Template::Context>.
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
166
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
167 =item * C<%args>
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
168
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
169 Именованные параметы загрузчика.
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
170
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
171 =over
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
172
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
173 =item * C<ext>
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
174
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
175 Расширение, которое будет добавляться к именам шаблонов и документов (если оно
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
176 не будет указано явно).
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
177
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
178 =item * C<initializer>
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
179
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
180 Имя шаблона, который будет использован для инициализации контекста.
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
181
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
182 =item * C<globals>
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
183
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
184 Глобальные переменнын, которые будут переданы в контекст.
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
185
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
186 =item * C<layoutBase>
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
187
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
188 Путь к шаблонам для оформления документов. Каждый документ может задавать свой
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
189 C<layout> указанный в его C<META> блоке или конструкторе.
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
190 См. C<IMPL::View::TTDocument>.
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
191
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
192 =back
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
193
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
194 =back
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
195
182
sergey
parents: 181
diff changeset
196 =head2 C<document($docName)>
sergey
parents: 181
diff changeset
197
265
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
198 Загружает документ с именем C<$docName>. При необходимости к нему будет
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
199 добавлено расширение, указанное в свойстве C<ext>. Это единственно полезный
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
200 метод провайдера.
6b6d4b2275a1 improved documentation
cin
parents: 232
diff changeset
201
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
202 =cut
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
203