Mercurial > pub > Impl
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 |
| rev | line source |
|---|---|
| 181 | 1 package IMPL::Web::View::TTDocument; |
| 2 use strict; | |
| 3 | |
| 236 | 4 use Scalar::Util qw(weaken); |
| 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 | 9 |
| 236 | 10 use IMPL::declare { |
| 11 require => { | |
| 12 TTFactory => 'IMPL::Web::View::TTFactory', | |
| 13 TTControl => 'IMPL::Web::View::TTControl', | |
| 14 Loader => 'IMPL::Code::Loader' | |
| 15 }, | |
| 16 base => [ | |
| 17 'IMPL::Web::View::TTControl' => sub { | |
| 287 | 18 my ($template,$ctx) = @_; |
|
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
19 $ctx ||= Template::Context->new(); |
| 300 | 20 return $template, $ctx; # context |
| 236 | 21 } |
| 22 ], | |
| 23 props => [ | |
| 24 layout => PROP_RW, | |
| 25 loader => PROP_RW, | |
| 26 ] | |
| 27 }; | |
| 181 | 28 |
| 29 sub CTOR { | |
| 287 | 30 my ($this,$template,$ctx,$loader,$vars) = @_; |
| 194 | 31 |
| 32 $this->loader($loader) if $loader; | |
| 33 $this->layout( $template->layout ) unless $this->layout; | |
| 301 | 34 $this->title( $template->title ) unless $this->title; |
| 194 | 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 | 38 } |
| 39 | |
| 40 sub Render { | |
| 194 | 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 | 47 my $documentContext; |
| 290 | 48 |
| 49 my $self = $this; | |
| 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 | 53 |
| 291 | 54 unless($self) { |
| 301 | 55 carp("Cant load control $control outside the document rendering process"); |
| 291 | 56 return; |
| 57 } | |
| 290 | 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 | 64 if ( my $template = $self->loader->template($path) ) { |
| 301 | 65 |
| 66 $documentContext->localise(); | |
| 67 my $ctx = _clone_context($documentContext); | |
| 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 | 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 | 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 | 87 $this->context->localise(); |
| 88 $documentContext = _clone_context( $this->context ); | |
| 290 | 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 | 96 my $tlayout = $this->loader->layout($this->layout); |
| 97 if(my $init = $tlayout->blocks->{INIT}) { | |
| 98 $this->context->process( | |
| 99 $init, | |
| 100 hashMerge( | |
|
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
101 $args, |
| 287 | 102 { |
|
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
103 template => $this->template |
| 287 | 104 } |
| 105 ) | |
| 106 ); | |
| 107 } | |
|
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
108 my $content = $this->next::method($args); |
| 287 | 109 return $this->context->include( |
| 110 $tlayout, | |
| 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 | 114 this => $this, |
| 115 template => $this->template | |
| 116 } | |
| 286 | 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 | 122 |
| 123 my $e = $@; | |
| 289 | 124 |
| 125 undef $require; | |
| 126 undef $documentContext; | |
| 127 undef %controls; | |
| 290 | 128 undef $self; |
| 289 | 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 | 133 } else { |
|
288
3a9cfea098dd
*TTView refactoring: removed RequireControl method, etc.
sergey
parents:
287
diff
changeset
|
134 return $text; |
| 194 | 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 | 144 sub _clone_context { |
| 145 my $args = { %{shift || {}} }; | |
| 146 delete $args->{CONFIG}; | |
| 147 | |
| 148 return Template::Context->new($args); | |
| 149 } | |
| 150 | |
| 181 | 151 1; |
| 152 | |
| 153 __END__ | |
| 154 | |
| 155 =pod | |
| 156 | |
| 157 =head1 NAME | |
| 158 | |
| 159 C<IMPL::Web::View::TTDocument> - документ для построения HTML страницы на основе шаблонов TT. | |
| 160 | |
| 161 =head1 SYNOPSIS | |
| 162 | |
| 163 =begin code | |
| 164 | |
| 165 | |
| 166 =end code | |
| 167 | |
| 168 =head1 DESCRIPTION | |
| 169 | |
| 170 | |
| 171 =over | |
| 172 | |
|
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
173 |
| 181 | 174 =cut |
