annotate Lib/IMPL/Web/View/TTContext.pm @ 347:3eafa6fefa9f

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