annotate Lib/IMPL/Web/View/TTContext.pm @ 346:f05634287ac7

working on the view concept
author cin
date Mon, 30 Sep 2013 01:37:50 +0400
parents 72799d1211c5
children 3eafa6fefa9f
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
346
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
90 object SharedContext {
345
cin
parents: 343
diff changeset
91 document
346
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
92 globals
345
cin
parents: 343
diff changeset
93 }
cin
parents: 343
diff changeset
94
cin
parents: 343
diff changeset
95 object DocumentContext {
346
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
96 base
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
97 extends
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
98 }
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
99
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
100 object ControlContext {
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
101 base
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
102 extends
345
cin
parents: 343
diff changeset
103 }
cin
parents: 343
diff changeset
104
346
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
105 SharedContext o-- DocumentContext
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
106 SharedContext o-- ControlContext
345
cin
parents: 343
diff changeset
107
346
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
108 Document -- DocumentContext
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
109 Control - ControlContext
345
cin
parents: 343
diff changeset
110
346
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
111 Loader . SharedContext: <<creates>>
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
112 Loader . Document: <<creates>>
f05634287ac7 working on the view concept
cin
parents: 345
diff changeset
113 Loader -up- Registry
345
cin
parents: 343
diff changeset
114
cin
parents: 343
diff changeset
115 @enduml
cin
parents: 343
diff changeset
116
cin
parents: 343
diff changeset
117 =end plantuml
cin
parents: 343
diff changeset
118
cin
parents: 343
diff changeset
119 =head1 MEMBERS
cin
parents: 343
diff changeset
120
cin
parents: 343
diff changeset
121 =head2 C<[get,set]base>
cin
parents: 343
diff changeset
122
cin
parents: 343
diff changeset
123 Префикс пути для поиска шаблонов
cin
parents: 343
diff changeset
124
cin
parents: 343
diff changeset
125 =head2 C<template($name)>
cin
parents: 343
diff changeset
126
cin
parents: 343
diff changeset
127 Сначала пытается загрузить шаблон используя префикс C<base>, затем без префикса.
cin
parents: 343
diff changeset
128
cin
parents: 343
diff changeset
129 =head2 C<clone()>
cin
parents: 343
diff changeset
130
cin
parents: 343
diff changeset
131 Создает копию контекста, при этом C<stash> локализуется, таким образом
cin
parents: 343
diff changeset
132 клонированный контекст имеет собственное пространство имен, вложенное в
cin
parents: 343
diff changeset
133 пространство родительского контекста.
cin
parents: 343
diff changeset
134
cin
parents: 343
diff changeset
135 =cut