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