annotate Lib/IMPL/Web/View/TTLoader.pm @ 191:78a18a2b6266

IMPL::Web::View improvements (unstable)
author cin
date Thu, 05 Apr 2012 17:51:51 +0400
parents ae8072f2f2a3
children 4d0e1962161c
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
185
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
80 return $name unless $this->ext;
ae8072f2f2a3 IMPL::Web::View::TTDocument tests, fixes
cin
parents: 184
diff changeset
81
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
82 if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
83 return $name;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
84 } else {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
85 return $name . $this->ext;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
86 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
87 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
88
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
89 sub _init {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
90 my ($this) = @_;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
91
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
92 if (!$this->isInitialized) {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
93 if ($this->initializer) {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
94 eval {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
95 $this->context->process($this->initializer);
184
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 182
diff changeset
96 };
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 182
diff changeset
97 if (my $e = $@) {
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 182
diff changeset
98 die new IMPL::Exception("Failed to process an initializer", $this->initializer, $e);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
99 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
100 }
184
7525ea9a071a IMPL::Web::View::TTLoader tests
sergey
parents: 182
diff changeset
101 $this->isInitialized(1);
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
102 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
103 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
104
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
105 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
106
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
107 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
108
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
109 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
110
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
111 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
112
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
113 C<IMPL::Web::View::TTLoader> - предоставляет глобальный контекст для загрузки шаблонов
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
114
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
115 =head1 SYNOPSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
116
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
117 =begin code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
118
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
119 use 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 my $loader = new IMPL::Web::View::TTLoader(
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
122 {
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
123 INCLUDE_PATH => [
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
124 '/my/app/tt',
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
125 '/my/app/tt/lib'
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 ext => '.tt',
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
129 initializer => 'shared/global'
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
130
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
131 );
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
132
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
133 my $doc = $loader->document('index');
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
134
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
135 my $html = $doc->Render();
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
136
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
137 =end code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
138
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
139 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
140
182
sergey
parents: 181
diff changeset
141 =head1 MEMBERS
sergey
parents: 181
diff changeset
142
sergey
parents: 181
diff changeset
143 =head2 C<document($docName)>
sergey
parents: 181
diff changeset
144
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
145 =cut
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
146