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