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