annotate Lib/IMPL/Web/View/TTDocument.pm @ 304:2ff513227cb4

*TTView: refactoring. Added control registry for the document.
author cin
date Mon, 15 Apr 2013 07:44:50 +0400
parents a5eb64c6e6f7
children b5d5793f348e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
1 package IMPL::Web::View::TTDocument;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
2 use strict;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
3
236
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
4 use Scalar::Util qw(weaken);
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
5 use IMPL::Const qw(:prop);
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
6 use IMPL::lang qw(:hash is);
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
7 use Carp qw(carp);
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
8 use mro;
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
9
236
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
10 use IMPL::declare {
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
11 require => {
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
12 TTFactory => 'IMPL::Web::View::TTFactory',
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
13 TTControl => 'IMPL::Web::View::TTControl',
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
14 Loader => 'IMPL::Code::Loader'
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
15 },
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
16 base => [
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
17 'IMPL::Web::View::TTControl' => sub {
287
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
18 my ($template,$ctx) = @_;
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
19 $ctx ||= Template::Context->new();
300
cin
parents: 298
diff changeset
20 return $template, $ctx; # context
236
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
21 }
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
22 ],
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
23 props => [
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
24 layout => PROP_RW,
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
25 loader => PROP_RW,
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
26 ]
2904da230022 DOM refactoring
sergey
parents: 234
diff changeset
27 };
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
28
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
29 sub CTOR {
287
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
30 my ($this,$template,$ctx,$loader,$vars) = @_;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 193
diff changeset
31
4d0e1962161c Replaced tabs with spaces
cin
parents: 193
diff changeset
32 $this->loader($loader) if $loader;
4d0e1962161c Replaced tabs with spaces
cin
parents: 193
diff changeset
33 $this->layout( $template->layout ) unless $this->layout;
301
aeeb57a12046 *IMPL::Web::View: templates inheritance support
cin
parents: 300
diff changeset
34 $this->title( $template->title ) unless $this->title;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 193
diff changeset
35
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
36 $this->context->stash->update($vars)
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
37 if ref $vars eq 'HASH';
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
38 }
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
39
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
40 sub Render {
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 193
diff changeset
41 my ($this,$args) = @_;
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
42
241
f48a1a9f4fa2 +Added ViewResult to allow implementation of the view environment.
sergey
parents: 238
diff changeset
43 $args ||= {};
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
44
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
45 my %controls;
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
46 my $require;
289
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
47 my $documentContext;
290
7b0dad6117d5 *TTView: fixed memory leak
sergey
parents: 289
diff changeset
48
7b0dad6117d5 *TTView: fixed memory leak
sergey
parents: 289
diff changeset
49 my $self = $this;
7b0dad6117d5 *TTView: fixed memory leak
sergey
parents: 289
diff changeset
50
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
51 $require = sub {
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
52 my $control = shift;
290
7b0dad6117d5 *TTView: fixed memory leak
sergey
parents: 289
diff changeset
53
291
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 290
diff changeset
54 unless($self) {
301
aeeb57a12046 *IMPL::Web::View: templates inheritance support
cin
parents: 300
diff changeset
55 carp("Cant load control $control outside the document rendering process");
291
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 290
diff changeset
56 return;
5d14baa35790 *TTView: fixed template selectors mechanism
cin
parents: 290
diff changeset
57 }
290
7b0dad6117d5 *TTView: fixed memory leak
sergey
parents: 289
diff changeset
58
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
59 if (my $factory = $controls{$control}) {
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
60 return $factory;
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
61 } else {
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
62 my $path = $control;
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
63
290
7b0dad6117d5 *TTView: fixed memory leak
sergey
parents: 289
diff changeset
64 if ( my $template = $self->loader->template($path) ) {
301
aeeb57a12046 *IMPL::Web::View: templates inheritance support
cin
parents: 300
diff changeset
65
aeeb57a12046 *IMPL::Web::View: templates inheritance support
cin
parents: 300
diff changeset
66 $documentContext->localise();
aeeb57a12046 *IMPL::Web::View: templates inheritance support
cin
parents: 300
diff changeset
67 my $ctx = _clone_context($documentContext);
aeeb57a12046 *IMPL::Web::View: templates inheritance support
cin
parents: 300
diff changeset
68 $documentContext->delocalise();
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
69
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
70 $factory = new IMPL::Web::View::TTFactory(
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
71 $template,
301
aeeb57a12046 *IMPL::Web::View: templates inheritance support
cin
parents: 300
diff changeset
72 $ctx,
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
73 join( '/', splice( @{[split(/\//,$path)]}, 0, -1 ) ),
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
74 $require
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
75 );
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
76
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
77 $controls{$control} = $factory;
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
78
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
79 return $factory;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 193
diff changeset
80
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
81 } else {
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
82 die new IMPL::KeyNotFoundException($control);
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
83 }
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
84 }
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
85 };
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
86
289
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
87 $this->context->localise();
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
88 $documentContext = _clone_context( $this->context );
290
7b0dad6117d5 *TTView: fixed memory leak
sergey
parents: 289
diff changeset
89
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
90 $this->context->stash->set(require => $require);
303
a5eb64c6e6f7 TTDocument.GetTemplate corrected to work with document blocks
cin
parents: 301
diff changeset
91 $this->context->stash->set(document => sub { $self });
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
92
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
93 my $text = eval {
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
94
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
95 if ($this->layout) {
287
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
96 my $tlayout = $this->loader->layout($this->layout);
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
97 if(my $init = $tlayout->blocks->{INIT}) {
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
98 $this->context->process(
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
99 $init,
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
100 hashMerge(
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
101 $args,
287
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
102 {
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
103 template => $this->template
287
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
104 }
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
105 )
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
106 );
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
107 }
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
108 my $content = $this->next::method($args);
287
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
109 return $this->context->include(
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
110 $tlayout,
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
111 {
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
112 %{$args},
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
113 content => $content,
287
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
114 this => $this,
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
115 template => $this->template
2d253e6e4a88 *TTView refactoring
cin
parents: 286
diff changeset
116 }
286
d357b5d85d25 *TTView refactoring
sergey
parents: 285
diff changeset
117 );
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
118 } else {
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
119 return $this->next::method($args);
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
120 }
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
121 };
301
aeeb57a12046 *IMPL::Web::View: templates inheritance support
cin
parents: 300
diff changeset
122
aeeb57a12046 *IMPL::Web::View: templates inheritance support
cin
parents: 300
diff changeset
123 my $e = $@;
289
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
124
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
125 undef $require;
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
126 undef $documentContext;
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
127 undef %controls;
290
7b0dad6117d5 *TTView: fixed memory leak
sergey
parents: 289
diff changeset
128 undef $self;
289
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
129 $this->context->delocalise();
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
130
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
131 if ($e) {
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
132 die $e;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 193
diff changeset
133 } else {
288
3a9cfea098dd *TTView refactoring: removed RequireControl method, etc.
sergey
parents: 287
diff changeset
134 return $text;
194
4d0e1962161c Replaced tabs with spaces
cin
parents: 193
diff changeset
135 }
190
cd1ff7029a63 IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents: 189
diff changeset
136 }
cd1ff7029a63 IMLP::Web::View refactored, added new method 'require' which is available inside templates. Changed document rendering.
cin
parents: 189
diff changeset
137
303
a5eb64c6e6f7 TTDocument.GetTemplate corrected to work with document blocks
cin
parents: 301
diff changeset
138 sub GetTemplate {
a5eb64c6e6f7 TTDocument.GetTemplate corrected to work with document blocks
cin
parents: 301
diff changeset
139 my ($this,$name) = @_;
a5eb64c6e6f7 TTDocument.GetTemplate corrected to work with document blocks
cin
parents: 301
diff changeset
140
a5eb64c6e6f7 TTDocument.GetTemplate corrected to work with document blocks
cin
parents: 301
diff changeset
141 $this->template->blocks->{$name};
a5eb64c6e6f7 TTDocument.GetTemplate corrected to work with document blocks
cin
parents: 301
diff changeset
142 }
a5eb64c6e6f7 TTDocument.GetTemplate corrected to work with document blocks
cin
parents: 301
diff changeset
143
289
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
144 sub _clone_context {
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
145 my $args = { %{shift || {}} };
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
146 delete $args->{CONFIG};
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
147
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
148 return Template::Context->new($args);
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
149 }
85572f512abc *TTView refactoring
cin
parents: 288
diff changeset
150
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
151 1;
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
152
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
153 __END__
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
154
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
155 =pod
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
156
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
157 =head1 NAME
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
158
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
159 C<IMPL::Web::View::TTDocument> - документ для построения HTML страницы на основе шаблонов TT.
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
160
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
161 =head1 SYNOPSIS
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
162
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
163 =begin code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
164
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
165
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
166 =end code
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
167
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
168 =head1 DESCRIPTION
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
169
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
170
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
171 =over
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
172
195
7a920771fd8e IMPL::Web::View changed document layout handling, docs, examples
cin
parents: 194
diff changeset
173
181
47dac58691ee New templating system, small fixes
sourcer
parents:
diff changeset
174 =cut