annotate Lib/IMPL/Web/View/TTLoader.pm @ 225:a1e868b0fba9

Bindings concept in progress
author sergey
date Fri, 31 Aug 2012 16:41:18 +0400
parents c8fe3f84feba
children 5c82eec23bb6
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 IMPL::lang qw(:declare :constants);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
5
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
6 use Template::Provider();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
7 use Template::Context();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
8 use Template::Constants qw(:status);
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
9
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
10 use IMPL::Web::View::TTDocument();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
11
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
12 use parent qw(
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
13 IMPL::Object
196
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
14 IMPL::Object::Serializable
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
15 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
16
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
17 BEGIN {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
18 public property options => PROP_ALL;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
19 public property provider => PROP_GET | PROP_OWNERSET;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
20 public property context => PROP_GET | PROP_OWNERSET;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
21 public property ext => PROP_ALL;
195
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
22 public property layoutBase => PROP_GET | PROP_OWNERSET;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
23
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
24 public property isInitialized => PROP_GET | PROP_OWNERSET;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
25 public property initializer => PROP_GET | PROP_OWNERSET;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
26
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
27 private property _globals => PROP_ALL;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
28 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
29
196
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
30 sub save {
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
31 my ($this,$context) = @_;
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
32
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
33 $context->AddVar($_, $this->$_()) for qw(options provider context ext layoutBase);
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
34 }
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
35
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
36 sub restore {
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
37 my ($class,$data,$surrogate) = @_;
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
38
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
39 my %params = @$data;
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
40
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
41 my $refOpts = delete $params{options};
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
42
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
43 if ($surrogate){
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
44 $surrogate->callCTOR($refOpts,%params);
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
45 } else {
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
46 $surrogate = $class->new($refOpts,%params);
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
47 }
206
c8fe3f84feba +IMPL::Web::Handlers::ViewSelector
sergey
parents: 205
diff changeset
48 return $surrogate;
196
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
49 }
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
50
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
51 sub CTOR {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
52 my ($this,$refOpts,%args) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
53
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
54 $refOpts ||= {};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
55
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
56 $this->ext($args{ext}) if $args{ext};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
57 $this->initializer($args{initializer}) if $args{initializer};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
58 $this->_globals(ref $args{globals} eq 'HASH' ? $args{globals} : {});
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
59
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
60 $this->options($refOpts);
195
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
61 $this->layoutBase($args{layoutBase}) if $args{layoutBase};
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
62
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
63 # to aviod cyclic references we need to do a copy of $refOpts
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
64 $refOpts->{LOAD_TEMPLATES} = $this->provider(new Template::Provider( { %$refOpts } ));
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
65
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
66 $this->context(new Template::Context($refOpts));
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
67 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
68
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
69 sub document {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
70 my ($this,$name,$vars) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
71
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
72 my $tt = $this->template($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
73
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
74 $this->_init();
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
75
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
76 my $opts = { %{ $this->options } };
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
77
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
78 $opts->{STASH} = $this->context->stash->clone();
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
79 $opts->{LOAD_TEMPLATES} = $this->provider;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
80
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
81 return new IMPL::Web::View::TTDocument( $tt, $opts, $this, $vars );
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
82 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
83
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
84 sub template {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
85 my ($this,$name) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
86
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
87 $name =~ s/^\s+|\s+$//g;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
88
205
891c04080658 IMPL::Web::View fixed template selection, release candidate
sergey
parents: 196
diff changeset
89 die new IMPL::InvalidArgumentException("A valid template name is required") unless length $name;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
90
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
91 $name = $this->_appendExt($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
92
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
93 my ($tt,$error) = $this->provider->fetch($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
94
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
95 if (defined $error and $error == STATUS_DECLINED) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
96 die new IMPL::KeyNotFoundException($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
97 } elsif (defined $error and $error == STATUS_ERROR) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
98 die new IMPL::Exception("Failed to load a template", $name, $tt);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
99 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
100
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
101 return $tt;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
102 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
103
195
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
104 sub layout {
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
105 my ($this,$name) = @_;
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
106
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
107 my $layout;
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
108
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
109 if ($this->layoutBase) {
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
110 $layout = $this->layoutBase . "/";
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
111 }
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
112
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
113 $layout .= $name;
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
114 return $this->template($layout);
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
115 }
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
116
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
117 sub _appendExt {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
118 my ($this,$name) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
119
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
120 return $name unless $this->ext;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
121
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
122 if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
123 return $name;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
124 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
125 return $name . $this->ext;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
126 }
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
127 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
128
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
129 sub _init {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
130 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
131
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
132 if (!$this->isInitialized) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
133 my $initializer = $this->initializer || sub {};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
134
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
135 eval {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
136 $this->context->process($initializer,$this->_globals);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
137 };
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
138 if (my $e = $@) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
139 die new IMPL::Exception("Failed to process an initializer", $this->initializer, $e);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
140 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
141
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
142 $this->isInitialized(1);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
143 }
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
144 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
145
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
146 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
147
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
148 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
149
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
150 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
151
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
152 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
153
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
154 C<IMPL::Web::View::TTLoader> - предоставляет глобальный контекст для загрузки шаблонов
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
155
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
156 =head1 SYNOPSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
157
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
158 =begin code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
159
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
160 use IMPL::Web::View::TTLoader();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
161
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
162 my $loader = new IMPL::Web::View::TTLoader(
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
163 {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
164 INCLUDE_PATH => [
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
165 '/my/app/tt',
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
166 '/my/app/tt/lib'
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
167 ]
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
168 },
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
169 ext => '.tt',
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
170 initializer => 'shared/global'
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
171
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
172 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
173
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
174 my $doc = $loader->document('index');
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
175
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
176 my $html = $doc->Render();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
177
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
178 =end code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
179
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
180 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
181
182
sergey
parents: 181
diff changeset
182 =head1 MEMBERS
sergey
parents: 181
diff changeset
183
sergey
parents: 181
diff changeset
184 =head2 C<document($docName)>
sergey
parents: 181
diff changeset
185
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
186 =cut
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
187