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