343
|
1 package IMPL::Web::View::TTContext;
|
|
2 use strict;
|
|
3 use Template::Base;
|
|
4
|
345
|
5 use IMPL::lang qw(is);
|
343
|
6 use IMPL::declare {
|
345
|
7 require => [
|
|
8 Document => '-Template::Document'
|
|
9 ],
|
343
|
10 base => {
|
|
11 'Template::Context' => '@_'
|
|
12 }
|
|
13 };
|
|
14
|
347
|
15 BEGIN {
|
|
16 no strict 'ref';
|
|
17 foreach my $prop (qw(
|
|
18 base
|
|
19 tt_ext
|
|
20 shared
|
|
21 parent
|
|
22 prefix
|
|
23 )) {
|
|
24 my $t = $prop;
|
|
25
|
|
26 *{__PACKAGE__ . '::' . $name} = sub {
|
|
27 my $this = shift;
|
|
28 return @_ ? $this->stash->set($t, @_) : $this->stash->get($t);
|
|
29 }
|
|
30 }
|
|
31 }
|
|
32
|
343
|
33 sub clone {
|
|
34 my $this = shift;
|
347
|
35 my $params = shift;
|
343
|
36
|
|
37 $this->localise();
|
|
38
|
|
39 my $args = { %{$this} };
|
|
40
|
|
41 $this->delocalise();
|
|
42
|
|
43 my $class = typeof($this);
|
|
44
|
|
45 delete $args->{CONFIG};
|
|
46
|
347
|
47 my $clone = $class->new($args);
|
|
48
|
|
49 $clone->stash->update($params) if $params;
|
|
50
|
|
51 return $clone;
|
345
|
52 }
|
|
53
|
|
54 sub find_template {
|
|
55 my ($this,$name,@inc) = @_;
|
|
56
|
|
57 push @inc, "";
|
|
58 my $ext = $this->tt_ext || "";
|
|
59
|
|
60 foreach my $dir (@inc) {
|
|
61 my $file = "$dir/$name$ext";
|
|
62 my $tt = eval { $this->template($file) };
|
|
63
|
|
64 return {
|
|
65 # if we load a block then we should use the current base directory
|
|
66 base => is($tt,Document) ? $dir : $this->base,
|
|
67 isDocument => is($tt,Document),
|
|
68 name => $name,
|
|
69 file => $file,
|
|
70 template => $tt
|
|
71 } if $tt;
|
|
72 }
|
|
73
|
|
74 $this->throw(Template::Constants::ERROR_FILE, "$name: not found");
|
|
75 }
|
|
76
|
|
77 sub require {
|
|
78 my ($this,$name) = @_;
|
|
79
|
|
80 return $this->stash->get([ 'require', [$name] ]);
|
|
81 }
|
|
82
|
347
|
83 sub display {
|
|
84 my $this = shift;
|
|
85 my $model = shift;
|
|
86 my $template, $args;
|
|
87
|
|
88 if (ref $_[0] eq 'HASH') {
|
|
89 $args = shift;
|
|
90 } else {
|
|
91 $template = shift;
|
|
92 $args = shift;
|
|
93 }
|
|
94
|
|
95 my $prefix = $this->prefix
|
|
96
|
|
97 my $cloned = $this->clone({
|
|
98 prefix => $prefix,
|
|
99 shared => $this->shared || $this,
|
|
100 parent => $this
|
|
101 });
|
|
102
|
|
103
|
|
104
|
|
105 }
|
|
106
|
345
|
107 1;
|
|
108
|
|
109 __END__
|
|
110
|
|
111 =pod
|
|
112
|
|
113 =head1 NAME
|
|
114
|
|
115 C<IMPL::Web::View::TTContext> - доработанная версия контекста
|
|
116
|
|
117 =head1 DESCRIPTION
|
|
118
|
|
119 Расширяет функции C<Template::Context>
|
|
120
|
|
121 =begin plantuml
|
|
122
|
|
123 @startuml
|
|
124
|
346
|
125 object SharedContext {
|
345
|
126 document
|
346
|
127 globals
|
345
|
128 }
|
|
129
|
|
130 object DocumentContext {
|
346
|
131 base
|
|
132 extends
|
|
133 }
|
|
134
|
|
135 object ControlContext {
|
|
136 base
|
|
137 extends
|
345
|
138 }
|
|
139
|
346
|
140 SharedContext o-- DocumentContext
|
|
141 SharedContext o-- ControlContext
|
345
|
142
|
346
|
143 Document -- DocumentContext
|
|
144 Control - ControlContext
|
345
|
145
|
346
|
146 Loader . SharedContext: <<creates>>
|
|
147 Loader . Document: <<creates>>
|
|
148 Loader -up- Registry
|
345
|
149
|
|
150 @enduml
|
|
151
|
|
152 =end plantuml
|
|
153
|
|
154 =head1 MEMBERS
|
|
155
|
|
156 =head2 C<[get,set]base>
|
|
157
|
|
158 Префикс пути для поиска шаблонов
|
|
159
|
|
160 =head2 C<template($name)>
|
|
161
|
|
162 Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса.
|
|
163
|
|
164 =head2 C<clone()>
|
|
165
|
|
166 Создает копию контекста, при этом C<stash> локализуется, таким образом
|
|
167 клонированный контекст имеет собственное пространство имен, вложенное в
|
|
168 пространство родительского контекста.
|
|
169
|
|
170 =cut |