annotate Lib/IMPL/Web/View/TTLoader.pm @ 194:4d0e1962161c

Replaced tabs with spaces IMPL::Web::View - fixed document model, new features (control classes, document constructor parameters)
author cin
date Tue, 10 Apr 2012 20:08:29 +0400
parents ae8072f2f2a3
children 7a920771fd8e
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;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
21
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
22 public property isInitialized => PROP_GET | PROP_OWNERSET;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
23 public property initializer => PROP_GET | PROP_OWNERSET;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
24
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
25 private property _globals => PROP_ALL;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
26 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
27
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
28 sub CTOR {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
29 my ($this,$refOpts,%args) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
30
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
31 $refOpts ||= {};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
32
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
33 $this->ext($args{ext}) if $args{ext};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
34 $this->initializer($args{initializer}) if $args{initializer};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
35 $this->_globals(ref $args{globals} eq 'HASH' ? $args{globals} : {});
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
36
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
37 $this->options($refOpts);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
38
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
39 # to aviod cyclic references we need to do a copy of $refOpts
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
40 $refOpts->{LOAD_TEMPLATES} = $this->provider(new Template::Provider( { %$refOpts } ));
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
41
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
42 $this->context(new Template::Context($refOpts));
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
43 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
44
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
45 sub document {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
46 my ($this,$name,$vars) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
47
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
48 my $tt = $this->template($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
49
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
50 $this->_init();
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
51
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
52 my $opts = { %{ $this->options } };
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
53
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
54 $opts->{STASH} = $this->context->stash->clone();
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
55 $opts->{LOAD_TEMPLATES} = $this->provider;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
56
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
57 return new IMPL::Web::View::TTDocument( $tt, $opts, $this, $vars );
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
58 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
59
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
60 sub template {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
61 my ($this,$name) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
62
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
63 $name =~ s/^\s+|\s+$//g;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
64
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
65 die new IMPL::ArgumentException("A valid template name is required") unless length $name;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
66
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
67 $name = $this->_appendExt($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
68
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
69 my ($tt,$error) = $this->provider->fetch($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
70
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
71 if (defined $error and $error == STATUS_DECLINED) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
72 die new IMPL::KeyNotFoundException($name);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
73 } elsif (defined $error and $error == STATUS_ERROR) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
74 die new IMPL::Exception("Failed to load a template", $name, $tt);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
75 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
76
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
77 return $tt;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
78 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
79
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
80 sub _appendExt {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
81 my ($this,$name) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
82
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
83 return $name unless $this->ext;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
84
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
85 if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
86 return $name;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
87 } else {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
88 return $name . $this->ext;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
89 }
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
90 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
91
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
92 sub _init {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
93 my ($this) = @_;
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
94
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
95 if (!$this->isInitialized) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
96 my $initializer = $this->initializer || sub {};
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
97
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
98 eval {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
99 $this->context->process($initializer,$this->_globals);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
100 };
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
101 if (my $e = $@) {
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
102 die new IMPL::Exception("Failed to process an initializer", $this->initializer, $e);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
103 }
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
104
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
105 $this->isInitialized(1);
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
106 }
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',
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
133 initializer => 'shared/global'
4d0e1962161c Replaced tabs with spaces
cin
parents: 185
diff changeset
134
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
135 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
136
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
137 my $doc = $loader->document('index');
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
138
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
139 my $html = $doc->Render();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
140
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
141 =end code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
142
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
143 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
144
182
sergey
parents: 181
diff changeset
145 =head1 MEMBERS
sergey
parents: 181
diff changeset
146
sergey
parents: 181
diff changeset
147 =head2 C<document($docName)>
sergey
parents: 181
diff changeset
148
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
149 =cut
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
150