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
|
|
80 if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) {
|
|
81 return $name;
|
|
82 } else {
|
|
83 return $name . $this->ext;
|
|
84 }
|
|
85 }
|
|
86
|
|
87 sub _init {
|
|
88 my ($this) = @_;
|
|
89
|
|
90 if (!$this->isInitialized) {
|
|
91 if ($this->initializer) {
|
|
92 eval {
|
|
93 $this->context->process($this->initializer);
|
184
|
94 };
|
|
95 if (my $e = $@) {
|
|
96 die new IMPL::Exception("Failed to process an initializer", $this->initializer, $e);
|
181
|
97 }
|
|
98 }
|
184
|
99 $this->isInitialized(1);
|
181
|
100 }
|
|
101 }
|
|
102
|
|
103 1;
|
|
104
|
|
105 __END__
|
|
106
|
|
107 =pod
|
|
108
|
|
109 =head1 NAME
|
|
110
|
|
111 C<IMPL::Web::View::TTLoader> - предоставляет глобальный контекст для загрузки шаблонов
|
|
112
|
|
113 =head1 SYNOPSIS
|
|
114
|
|
115 =begin code
|
|
116
|
|
117 use IMPL::Web::View::TTLoader();
|
|
118
|
|
119 my $loader = new IMPL::Web::View::TTLoader(
|
|
120 {
|
|
121 INCLUDE_PATH => [
|
|
122 '/my/app/tt',
|
|
123 '/my/app/tt/lib'
|
|
124 ]
|
|
125 },
|
|
126 ext => '.tt',
|
|
127 initializer => 'shared/global'
|
|
128
|
|
129 );
|
|
130
|
|
131 my $doc = $loader->document('index');
|
|
132
|
|
133 my $html = $doc->Render();
|
|
134
|
|
135 =end code
|
|
136
|
|
137 =head1 DESCRIPTION
|
|
138
|
182
|
139 =head1 MEMBERS
|
|
140
|
|
141 =head2 C<document($docName)>
|
|
142
|
181
|
143 =cut
|
|
144
|