annotate Lib/IMPL/Web/View/TTLoader.pm @ 196:a705e848dcc7

added IMPL::Config::Reference
author cin
date Mon, 16 Apr 2012 17:42:54 +0400
parents 7a920771fd8e
children 891c04080658
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 }
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
48 }
a705e848dcc7 added IMPL::Config::Reference
cin
parents: 195
diff changeset
49
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
50 sub CTOR {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
51 my ($this,$refOpts,%args) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
52
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
53 $refOpts ||= {};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
54
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
55 $this->ext($args{ext}) if $args{ext};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
56 $this->initializer($args{initializer}) if $args{initializer};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
57 $this->_globals(ref $args{globals} eq 'HASH' ? $args{globals} : {});
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
58
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
59 $this->options($refOpts);
195
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
60 $this->layoutBase($args{layoutBase}) if $args{layoutBase};
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
61
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
62 # to aviod cyclic references we need to do a copy of $refOpts
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
63 $refOpts->{LOAD_TEMPLATES} = $this->provider(new Template::Provider( { %$refOpts } ));
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
64
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
65 $this->context(new Template::Context($refOpts));
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
66 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
67
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
68 sub document {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
69 my ($this,$name,$vars) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
70
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
71 my $tt = $this->template($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
72
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
73 $this->_init();
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
74
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
75 my $opts = { %{ $this->options } };
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
76
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
77 $opts->{STASH} = $this->context->stash->clone();
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
78 $opts->{LOAD_TEMPLATES} = $this->provider;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
79
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
80 return new IMPL::Web::View::TTDocument( $tt, $opts, $this, $vars );
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
81 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
82
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
83 sub template {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
84 my ($this,$name) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
85
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
86 $name =~ s/^\s+|\s+$//g;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
87
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
88 die new IMPL::ArgumentException("A valid template name is required") unless length $name;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
89
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
90 $name = $this->_appendExt($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
91
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
92 my ($tt,$error) = $this->provider->fetch($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
93
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
94 if (defined $error and $error == STATUS_DECLINED) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
95 die new IMPL::KeyNotFoundException($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
96 } elsif (defined $error and $error == STATUS_ERROR) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
97 die new IMPL::Exception("Failed to load a template", $name, $tt);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
98 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
99
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
100 return $tt;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
101 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
102
195
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
103 sub layout {
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
104 my ($this,$name) = @_;
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
105
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
106 my $layout;
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
107
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
108 if ($this->layoutBase) {
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
109 $layout = $this->layoutBase . "/";
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
110 }
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 $layout .= $name;
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
113 return $this->template($layout);
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
114 }
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
115
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
116 sub _appendExt {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
117 my ($this,$name) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
118
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
119 return $name unless $this->ext;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
120
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
121 if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
122 return $name;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
123 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
124 return $name . $this->ext;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
125 }
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
126 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
127
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
128 sub _init {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
129 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
130
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
131 if (!$this->isInitialized) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
132 my $initializer = $this->initializer || sub {};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
133
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
134 eval {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
135 $this->context->process($initializer,$this->_globals);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
136 };
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
137 if (my $e = $@) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
138 die new IMPL::Exception("Failed to process an initializer", $this->initializer, $e);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
139 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
140
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
141 $this->isInitialized(1);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
142 }
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
143 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
144
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
145 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
146
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
147 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
148
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
149 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
150
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
151 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
152
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
153 C<IMPL::Web::View::TTLoader> - предоставляет глобальный контекст для загрузки шаблонов
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
154
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
155 =head1 SYNOPSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
156
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
157 =begin code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
158
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
159 use IMPL::Web::View::TTLoader();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
160
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
161 my $loader = new IMPL::Web::View::TTLoader(
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
162 {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
163 INCLUDE_PATH => [
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
164 '/my/app/tt',
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
165 '/my/app/tt/lib'
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
166 ]
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
167 },
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
168 ext => '.tt',
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
169 initializer => 'shared/global'
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
170
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
171 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
172
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
173 my $doc = $loader->document('index');
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
174
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
175 my $html = $doc->Render();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
176
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
177 =end code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
178
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
179 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
180
182
sergey
parents: 181
diff changeset
181 =head1 MEMBERS
sergey
parents: 181
diff changeset
182
sergey
parents: 181
diff changeset
183 =head2 C<document($docName)>
sergey
parents: 181
diff changeset
184
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
185 =cut
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
186