annotate Lib/IMPL/Web/View/TTLoader.pm @ 182:adc7669172c4

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