annotate Lib/IMPL/Web/View/TTLoader.pm @ 184:7525ea9a071a

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