annotate Lib/IMPL/Web/View/TTLoader.pm @ 195:7a920771fd8e

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