comparison Lib/IMPL/Web/View/TTDocument.pm @ 289:85572f512abc

*TTView refactoring
author cin
date Wed, 20 Feb 2013 02:25:30 +0400
parents 3a9cfea098dd
children 7b0dad6117d5
comparison
equal deleted inserted replaced
288:3a9cfea098dd 289:85572f512abc
21 } 21 }
22 ], 22 ],
23 props => [ 23 props => [
24 layout => PROP_RW, 24 layout => PROP_RW,
25 loader => PROP_RW, 25 loader => PROP_RW,
26 controls => PROP_RO,
27 ] 26 ]
28 }; 27 };
29 28
30 sub CTOR { 29 sub CTOR {
31 my ($this,$template,$ctx,$loader,$vars) = @_; 30 my ($this,$template,$ctx,$loader,$vars) = @_;
39 if ref $vars eq 'HASH'; 38 if ref $vars eq 'HASH';
40 39
41 $this->InitInstance(); 40 $this->InitInstance();
42 } 41 }
43 42
44 sub templateVars {
45 my $this = shift;
46 my $name = shift;
47 if (@_) {
48 return $this->context->stash->set($name, shift);
49 } else {
50 return $this->context->stash->get($name);
51 }
52 }
53
54 sub RequireControl {
55 my ($this, $control, $ctx) = @_;
56
57 $ctx ||= $this->context;
58
59 if (my $factory = $this->controls->{$control}) {
60 return $factory;
61 } else {
62 my $path = $control;
63
64 if ( my $template = $this->loader->template($path) ) {
65
66 $factory = new IMPL::Web::View::TTFactory(
67 $template->class || TTControl,
68 $template,
69 $ctx,
70 join( '/', splice( @{[split(/\//,$path)]}, 0, -1 ) )
71 );
72
73 if ($template->class) {
74 Loader->safe->Require($template->class);
75 }
76
77 $this->controls->{$control} = $factory;
78
79 return $factory;
80
81 } else {
82 die new IMPL::KeyNotFoundException($control);
83 }
84 }
85 }
86
87 sub Render { 43 sub Render {
88 my ($this,$args) = @_; 44 my ($this,$args) = @_;
89 45
90 $args ||= {}; 46 $args ||= {};
91 47
92 $this->context->localise();
93
94 my $documentContext;
95 my %controls; 48 my %controls;
96 my $require; 49 my $require;
50 my $documentContext;
97 $require = sub { 51 $require = sub {
98 my $control = shift; 52 my $control = shift;
99 if (my $factory = $controls{$control}) { 53 if (my $factory = $controls{$control}) {
100 return $factory; 54 return $factory;
101 } else { 55 } else {
123 die new IMPL::KeyNotFoundException($control); 77 die new IMPL::KeyNotFoundException($control);
124 } 78 }
125 } 79 }
126 }; 80 };
127 81
82 $this->context->localise();
83 $documentContext = _clone_context( $this->context );
84 my $self = $this;
85 weaken($self);
128 $this->context->stash->set(require => $require); 86 $this->context->stash->set(require => $require);
129 $this->context->stash->set(document => $this); 87 #$this->context->stash->set(document => sub { $self });
130 $documentContext = Template::Context->new( { %{$this->context} } ); 88
131 89
132 my $text = eval { 90 my $text = eval {
133 91
134 if ($this->layout) { 92 if ($this->layout) {
135 my $tlayout = $this->loader->layout($this->layout); 93 my $tlayout = $this->loader->layout($this->layout);
156 ); 114 );
157 } else { 115 } else {
158 return $this->next::method($args); 116 return $this->next::method($args);
159 } 117 }
160 }; 118 };
161 119
120 undef $require;
121 undef $documentContext;
122 undef %controls;
162 $this->context->delocalise(); 123 $this->context->delocalise();
124
163 125
164 my $e = $@; 126 my $e = $@;
165 if ($e) { 127 if ($e) {
166 die $e; 128 die $e;
167 } else { 129 } else {
168 return $text; 130 return $text;
169 } 131 }
170 132
171 133
134 }
135
136 sub _clone_context {
137 my $args = { %{shift || {}} };
138 delete $args->{CONFIG};
139
140 return Template::Context->new($args);
172 } 141 }
173 142
174 1; 143 1;
175 144
176 __END__ 145 __END__