annotate Lib/IMPL/Web/View/TTContext.pm @ 345:72799d1211c5

sync
author cin
date Fri, 27 Sep 2013 16:28:27 +0400
parents 9bdccdf1f50b
children f05634287ac7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
343
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
1 package IMPL::Web::View::TTContext;
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
2 use strict;
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
3 use Template::Base;
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
4
345
cin
parents: 343
diff changeset
5 use IMPL::lang qw(is);
343
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
6 use IMPL::declare {
345
cin
parents: 343
diff changeset
7 require => [
cin
parents: 343
diff changeset
8 Document => '-Template::Document'
cin
parents: 343
diff changeset
9 ],
343
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
10 base => {
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
11 'Template::Context' => '@_'
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
12 }
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
13 };
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
14
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
15 sub clone {
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
16 my $this = shift;
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
17
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
18 $this->localise();
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
19
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
20 my $args = { %{$this} };
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
21
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
22 $this->delocalise();
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
23
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
24 my $class = typeof($this);
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
25
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
26 delete $args->{CONFIG};
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
27
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
28 return $class->new($args);
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
29 }
9bdccdf1f50b Added a templates context
cin
parents:
diff changeset
30
345
cin
parents: 343
diff changeset
31 sub base {
cin
parents: 343
diff changeset
32 my $this = shift;
cin
parents: 343
diff changeset
33
cin
parents: 343
diff changeset
34 return @_ ? $this->stash->set('base', @_) : $this->stash->get('base');
cin
parents: 343
diff changeset
35 }
cin
parents: 343
diff changeset
36
cin
parents: 343
diff changeset
37 sub tt_ext {
cin
parents: 343
diff changeset
38 my $this = shift;
cin
parents: 343
diff changeset
39
cin
parents: 343
diff changeset
40 return @_ ? $this->stash->set('tt_ext', @_) : $this->stash->get('tt_ext');
cin
parents: 343
diff changeset
41 }
cin
parents: 343
diff changeset
42
cin
parents: 343
diff changeset
43 sub find_template {
cin
parents: 343
diff changeset
44 my ($this,$name,@inc) = @_;
cin
parents: 343
diff changeset
45
cin
parents: 343
diff changeset
46 push @inc, "";
cin
parents: 343
diff changeset
47 my $ext = $this->tt_ext || "";
cin
parents: 343
diff changeset
48
cin
parents: 343
diff changeset
49 foreach my $dir (@inc) {
cin
parents: 343
diff changeset
50 my $file = "$dir/$name$ext";
cin
parents: 343
diff changeset
51 my $tt = eval { $this->template($file) };
cin
parents: 343
diff changeset
52
cin
parents: 343
diff changeset
53 return {
cin
parents: 343
diff changeset
54 # if we load a block then we should use the current base directory
cin
parents: 343
diff changeset
55 base => is($tt,Document) ? $dir : $this->base,
cin
parents: 343
diff changeset
56 isDocument => is($tt,Document),
cin
parents: 343
diff changeset
57 name => $name,
cin
parents: 343
diff changeset
58 file => $file,
cin
parents: 343
diff changeset
59 template => $tt
cin
parents: 343
diff changeset
60 } if $tt;
cin
parents: 343
diff changeset
61 }
cin
parents: 343
diff changeset
62
cin
parents: 343
diff changeset
63 $this->throw(Template::Constants::ERROR_FILE, "$name: not found");
cin
parents: 343
diff changeset
64 }
cin
parents: 343
diff changeset
65
cin
parents: 343
diff changeset
66 sub require {
cin
parents: 343
diff changeset
67 my ($this,$name) = @_;
cin
parents: 343
diff changeset
68
cin
parents: 343
diff changeset
69 return $this->stash->get([ 'require', [$name] ]);
cin
parents: 343
diff changeset
70 }
cin
parents: 343
diff changeset
71
cin
parents: 343
diff changeset
72 1;
cin
parents: 343
diff changeset
73
cin
parents: 343
diff changeset
74 __END__
cin
parents: 343
diff changeset
75
cin
parents: 343
diff changeset
76 =pod
cin
parents: 343
diff changeset
77
cin
parents: 343
diff changeset
78 =head1 NAME
cin
parents: 343
diff changeset
79
cin
parents: 343
diff changeset
80 C<IMPL::Web::View::TTContext> - доработанная версия контекста
cin
parents: 343
diff changeset
81
cin
parents: 343
diff changeset
82 =head1 DESCRIPTION
cin
parents: 343
diff changeset
83
cin
parents: 343
diff changeset
84 Расширяет функции C<Template::Context>
cin
parents: 343
diff changeset
85
cin
parents: 343
diff changeset
86 =begin plantuml
cin
parents: 343
diff changeset
87
cin
parents: 343
diff changeset
88 @startuml
cin
parents: 343
diff changeset
89
cin
parents: 343
diff changeset
90 object FooContext {
cin
parents: 343
diff changeset
91 document
cin
parents: 343
diff changeset
92 this
cin
parents: 343
diff changeset
93 model
cin
parents: 343
diff changeset
94 }
cin
parents: 343
diff changeset
95
cin
parents: 343
diff changeset
96 object BarContext {
cin
parents: 343
diff changeset
97 document
cin
parents: 343
diff changeset
98 this
cin
parents: 343
diff changeset
99 model
cin
parents: 343
diff changeset
100 }
cin
parents: 343
diff changeset
101
cin
parents: 343
diff changeset
102 object FooFactoryContext {
cin
parents: 343
diff changeset
103 require = function(){}
cin
parents: 343
diff changeset
104 include = function(){}
cin
parents: 343
diff changeset
105 labels = {...}
cin
parents: 343
diff changeset
106 base = "my/app/view"
cin
parents: 343
diff changeset
107 extends = "my/app/view/bar"
cin
parents: 343
diff changeset
108 }
cin
parents: 343
diff changeset
109
cin
parents: 343
diff changeset
110 object BarFactoryContext {
cin
parents: 343
diff changeset
111 require = function(){}
cin
parents: 343
diff changeset
112 include = function(){}
cin
parents: 343
diff changeset
113 base = "my/app/view"
cin
parents: 343
diff changeset
114 labels = {...}
cin
parents: 343
diff changeset
115 extends = undefined
cin
parents: 343
diff changeset
116 }
cin
parents: 343
diff changeset
117
cin
parents: 343
diff changeset
118 object RegistryContext {
cin
parents: 343
diff changeset
119 registry
cin
parents: 343
diff changeset
120 }
cin
parents: 343
diff changeset
121
cin
parents: 343
diff changeset
122 object DocumentFactoryContext {
cin
parents: 343
diff changeset
123 require = function() {}
cin
parents: 343
diff changeset
124 include = function() {}
cin
parents: 343
diff changeset
125 labels = {...}
cin
parents: 343
diff changeset
126 base = ""
cin
parents: 343
diff changeset
127 extends = undefined
cin
parents: 343
diff changeset
128 }
cin
parents: 343
diff changeset
129
cin
parents: 343
diff changeset
130 object DocumentContext {
cin
parents: 343
diff changeset
131 this
cin
parents: 343
diff changeset
132 }
cin
parents: 343
diff changeset
133
cin
parents: 343
diff changeset
134 FooFactoryContext --o BarFactoryContext
cin
parents: 343
diff changeset
135 BarFactoryContext --o RegistryContext
cin
parents: 343
diff changeset
136
cin
parents: 343
diff changeset
137 FooContext -right-o FooFactoryContext
cin
parents: 343
diff changeset
138 BarContext -right-o BarFactoryContext
cin
parents: 343
diff changeset
139
cin
parents: 343
diff changeset
140 DocumentFactoryContext -up-o RegistryContext
cin
parents: 343
diff changeset
141 DocumentContext -left-o DocumentFactoryContext
cin
parents: 343
diff changeset
142
cin
parents: 343
diff changeset
143 Document --> DocumentContext
cin
parents: 343
diff changeset
144
cin
parents: 343
diff changeset
145 @enduml
cin
parents: 343
diff changeset
146
cin
parents: 343
diff changeset
147 =end plantuml
cin
parents: 343
diff changeset
148
cin
parents: 343
diff changeset
149 =head1 MEMBERS
cin
parents: 343
diff changeset
150
cin
parents: 343
diff changeset
151 =head2 C<[get,set]base>
cin
parents: 343
diff changeset
152
cin
parents: 343
diff changeset
153 Префикс пути для поиска шаблонов
cin
parents: 343
diff changeset
154
cin
parents: 343
diff changeset
155 =head2 C<template($name)>
cin
parents: 343
diff changeset
156
cin
parents: 343
diff changeset
157 Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса.
cin
parents: 343
diff changeset
158
cin
parents: 343
diff changeset
159 =head2 C<clone()>
cin
parents: 343
diff changeset
160
cin
parents: 343
diff changeset
161 Создает копию контекста, при этом C<stash> локализуется, таким образом
cin
parents: 343
diff changeset
162 клонированный контекст имеет собственное пространство имен, вложенное в
cin
parents: 343
diff changeset
163 пространство родительского контекста.
cin
parents: 343
diff changeset
164
cin
parents: 343
diff changeset
165 =cut