comparison Lib/IMPL/Web/View/TTLoader.pm @ 194:4d0e1962161c

Replaced tabs with spaces IMPL::Web::View - fixed document model, new features (control classes, document constructor parameters)
author cin
date Tue, 10 Apr 2012 20:08:29 +0400
parents ae8072f2f2a3
children 7a920771fd8e
comparison
equal deleted inserted replaced
193:8e8401c0aea4 194:4d0e1962161c
8 use Template::Constants qw(:status); 8 use Template::Constants qw(:status);
9 9
10 use IMPL::Web::View::TTDocument(); 10 use IMPL::Web::View::TTDocument();
11 11
12 use parent qw( 12 use parent qw(
13 IMPL::Object 13 IMPL::Object
14 ); 14 );
15 15
16 BEGIN { 16 BEGIN {
17 public property options => PROP_ALL; 17 public property options => PROP_ALL;
18 public property provider => PROP_GET | PROP_OWNERSET; 18 public property provider => PROP_GET | PROP_OWNERSET;
19 public property context => PROP_GET | PROP_OWNERSET; 19 public property context => PROP_GET | PROP_OWNERSET;
20 public property ext => PROP_ALL; 20 public property ext => PROP_ALL;
21 21
22 public property isInitialized => PROP_GET | PROP_OWNERSET; 22 public property isInitialized => PROP_GET | PROP_OWNERSET;
23 public property initializer => PROP_GET | PROP_OWNERSET; 23 public property initializer => PROP_GET | PROP_OWNERSET;
24
25 private property _globals => PROP_ALL;
24 } 26 }
25 27
26 sub CTOR { 28 sub CTOR {
27 my ($this,$refOpts,%args) = @_; 29 my ($this,$refOpts,%args) = @_;
28 30
29 $refOpts ||= {}; 31 $refOpts ||= {};
30 32
31 $this->ext(delete $args{ext}); 33 $this->ext($args{ext}) if $args{ext};
32 $this->initializer(delete $args{initializer}); 34 $this->initializer($args{initializer}) if $args{initializer};
33 35 $this->_globals(ref $args{globals} eq 'HASH' ? $args{globals} : {});
34 $this->options($refOpts); 36
35 37 $this->options($refOpts);
36 # to aviod cyclic references we need to do a copy of $refOpts 38
37 $refOpts->{LOAD_TEMPLATES} = $this->provider(new Template::Provider( { %$refOpts } )); 39 # to aviod cyclic references we need to do a copy of $refOpts
38 40 $refOpts->{LOAD_TEMPLATES} = $this->provider(new Template::Provider( { %$refOpts } ));
39 $this->context(new Template::Context($refOpts)); 41
42 $this->context(new Template::Context($refOpts));
40 } 43 }
41 44
42 sub document { 45 sub document {
43 my ($this,$name) = @_; 46 my ($this,$name,$vars) = @_;
44 47
45 my $tt = $this->template($name); 48 my $tt = $this->template($name);
46 49
47 $this->_init(); 50 $this->_init();
48 51
49 my $opts = { %{ $this->options } }; 52 my $opts = { %{ $this->options } };
50 53
51 $opts->{STASH} = $this->context->stash->clone(); 54 $opts->{STASH} = $this->context->stash->clone();
52 $opts->{LOAD_TEMPLATES} = $this->provider; 55 $opts->{LOAD_TEMPLATES} = $this->provider;
53 56
54 return new IMPL::Web::View::TTDocument( $tt, $opts, loader => $this ); 57 return new IMPL::Web::View::TTDocument( $tt, $opts, $this, $vars );
55 } 58 }
56 59
57 sub template { 60 sub template {
58 my ($this,$name) = @_; 61 my ($this,$name) = @_;
59 62
60 $name =~ s/^\s+|\s+$//g; 63 $name =~ s/^\s+|\s+$//g;
61 64
62 die new IMPL::ArgumentException("A valid template name is required") unless length $name; 65 die new IMPL::ArgumentException("A valid template name is required") unless length $name;
63 66
64 $name = $this->_appendExt($name); 67 $name = $this->_appendExt($name);
65 68
66 my ($tt,$error) = $this->provider->fetch($name); 69 my ($tt,$error) = $this->provider->fetch($name);
67 70
68 if (defined $error and $error == STATUS_DECLINED) { 71 if (defined $error and $error == STATUS_DECLINED) {
69 die new IMPL::KeyNotFoundException($name); 72 die new IMPL::KeyNotFoundException($name);
70 } elsif (defined $error and $error == STATUS_ERROR) { 73 } elsif (defined $error and $error == STATUS_ERROR) {
71 die new IMPL::Exception("Failed to load a template", $name, $tt); 74 die new IMPL::Exception("Failed to load a template", $name, $tt);
72 } 75 }
73 76
74 return $tt; 77 return $tt;
75 } 78 }
76 79
77 sub _appendExt { 80 sub _appendExt {
78 my ($this,$name) = @_; 81 my ($this,$name) = @_;
79 82
80 return $name unless $this->ext; 83 return $name unless $this->ext;
81 84
82 if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) { 85 if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) {
83 return $name; 86 return $name;
84 } else { 87 } else {
85 return $name . $this->ext; 88 return $name . $this->ext;
86 } 89 }
87 } 90 }
88 91
89 sub _init { 92 sub _init {
90 my ($this) = @_; 93 my ($this) = @_;
91 94
92 if (!$this->isInitialized) { 95 if (!$this->isInitialized) {
93 if ($this->initializer) { 96 my $initializer = $this->initializer || sub {};
94 eval { 97
95 $this->context->process($this->initializer); 98 eval {
96 }; 99 $this->context->process($initializer,$this->_globals);
97 if (my $e = $@) { 100 };
98 die new IMPL::Exception("Failed to process an initializer", $this->initializer, $e); 101 if (my $e = $@) {
99 } 102 die new IMPL::Exception("Failed to process an initializer", $this->initializer, $e);
100 } 103 }
101 $this->isInitialized(1); 104
102 } 105 $this->isInitialized(1);
106 }
103 } 107 }
104 108
105 1; 109 1;
106 110
107 __END__ 111 __END__
117 =begin code 121 =begin code
118 122
119 use IMPL::Web::View::TTLoader(); 123 use IMPL::Web::View::TTLoader();
120 124
121 my $loader = new IMPL::Web::View::TTLoader( 125 my $loader = new IMPL::Web::View::TTLoader(
122 { 126 {
123 INCLUDE_PATH => [ 127 INCLUDE_PATH => [
124 '/my/app/tt', 128 '/my/app/tt',
125 '/my/app/tt/lib' 129 '/my/app/tt/lib'
126 ] 130 ]
127 }, 131 },
128 ext => '.tt', 132 ext => '.tt',
129 initializer => 'shared/global' 133 initializer => 'shared/global'
130 134
131 ); 135 );
132 136
133 my $doc = $loader->document('index'); 137 my $doc = $loader->document('index');
134 138
135 my $html = $doc->Render(); 139 my $html = $doc->Render();