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
|
|
15 sub clone {
|
|
16 my $this = shift;
|
|
17
|
|
18 $this->localise();
|
|
19
|
|
20 my $args = { %{$this} };
|
|
21
|
|
22 $this->delocalise();
|
|
23
|
|
24 my $class = typeof($this);
|
|
25
|
|
26 delete $args->{CONFIG};
|
|
27
|
|
28 return $class->new($args);
|
|
29 }
|
|
30
|
345
|
31 sub base {
|
|
32 my $this = shift;
|
|
33
|
|
34 return @_ ? $this->stash->set('base', @_) : $this->stash->get('base');
|
|
35 }
|
|
36
|
|
37 sub tt_ext {
|
|
38 my $this = shift;
|
|
39
|
|
40 return @_ ? $this->stash->set('tt_ext', @_) : $this->stash->get('tt_ext');
|
|
41 }
|
|
42
|
|
43 sub find_template {
|
|
44 my ($this,$name,@inc) = @_;
|
|
45
|
|
46 push @inc, "";
|
|
47 my $ext = $this->tt_ext || "";
|
|
48
|
|
49 foreach my $dir (@inc) {
|
|
50 my $file = "$dir/$name$ext";
|
|
51 my $tt = eval { $this->template($file) };
|
|
52
|
|
53 return {
|
|
54 # if we load a block then we should use the current base directory
|
|
55 base => is($tt,Document) ? $dir : $this->base,
|
|
56 isDocument => is($tt,Document),
|
|
57 name => $name,
|
|
58 file => $file,
|
|
59 template => $tt
|
|
60 } if $tt;
|
|
61 }
|
|
62
|
|
63 $this->throw(Template::Constants::ERROR_FILE, "$name: not found");
|
|
64 }
|
|
65
|
|
66 sub require {
|
|
67 my ($this,$name) = @_;
|
|
68
|
|
69 return $this->stash->get([ 'require', [$name] ]);
|
|
70 }
|
|
71
|
|
72 1;
|
|
73
|
|
74 __END__
|
|
75
|
|
76 =pod
|
|
77
|
|
78 =head1 NAME
|
|
79
|
|
80 C<IMPL::Web::View::TTContext> - доработанная версия контекста
|
|
81
|
|
82 =head1 DESCRIPTION
|
|
83
|
|
84 Расширяет функции C<Template::Context>
|
|
85
|
|
86 =begin plantuml
|
|
87
|
|
88 @startuml
|
|
89
|
346
|
90 object SharedContext {
|
345
|
91 document
|
346
|
92 globals
|
345
|
93 }
|
|
94
|
|
95 object DocumentContext {
|
346
|
96 base
|
|
97 extends
|
|
98 }
|
|
99
|
|
100 object ControlContext {
|
|
101 base
|
|
102 extends
|
345
|
103 }
|
|
104
|
346
|
105 SharedContext o-- DocumentContext
|
|
106 SharedContext o-- ControlContext
|
345
|
107
|
346
|
108 Document -- DocumentContext
|
|
109 Control - ControlContext
|
345
|
110
|
346
|
111 Loader . SharedContext: <<creates>>
|
|
112 Loader . Document: <<creates>>
|
|
113 Loader -up- Registry
|
345
|
114
|
|
115 @enduml
|
|
116
|
|
117 =end plantuml
|
|
118
|
|
119 =head1 MEMBERS
|
|
120
|
|
121 =head2 C<[get,set]base>
|
|
122
|
|
123 Префикс пути для поиска шаблонов
|
|
124
|
|
125 =head2 C<template($name)>
|
|
126
|
|
127 Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса.
|
|
128
|
|
129 =head2 C<clone()>
|
|
130
|
|
131 Создает копию контекста, при этом C<stash> локализуется, таким образом
|
|
132 клонированный контекст имеет собственное пространство имен, вложенное в
|
|
133 пространство родительского контекста.
|
|
134
|
|
135 =cut |