Mercurial > pub > Impl
comparison Lib/IMPL/Web/View/TTLoader.pm @ 184:7525ea9a071a
IMPL::Web::View::TTLoader tests
| author | sergey |
|---|---|
| date | Thu, 29 Mar 2012 01:54:20 +0400 |
| parents | adc7669172c4 |
| children | ae8072f2f2a3 |
comparison
equal
deleted
inserted
replaced
| 183:2184fa28b49e | 184:7525ea9a071a |
|---|---|
| 24 } | 24 } |
| 25 | 25 |
| 26 sub CTOR { | 26 sub CTOR { |
| 27 my ($this,$refOpts,%args) = @_; | 27 my ($this,$refOpts,%args) = @_; |
| 28 | 28 |
| 29 $this->ext(delete $args{etx}); | 29 $refOpts ||= {}; |
| 30 | |
| 31 $this->ext(delete $args{ext}); | |
| 30 $this->initializer(delete $args{initializer}); | 32 $this->initializer(delete $args{initializer}); |
| 31 | 33 |
| 32 $this->options($refOpts); | 34 $this->options($refOpts); |
| 33 | 35 |
| 34 $refOpts->{LOAD_TEMPLATES} = $this->provider(new Template::Provider($refOpts)); | 36 # to aviod cyclic references we need to do a copy of $refOpts |
| 37 $refOpts->{LOAD_TEMPLATES} = $this->provider(new Template::Provider( { %$refOpts } )); | |
| 35 | 38 |
| 36 $this->context(new Template::Context($refOpts)); | 39 $this->context(new Template::Context($refOpts)); |
| 37 } | 40 } |
| 38 | 41 |
| 39 sub document { | 42 sub document { |
| 41 | 44 |
| 42 my $tt = $this->template($name); | 45 my $tt = $this->template($name); |
| 43 | 46 |
| 44 $this->_init(); | 47 $this->_init(); |
| 45 | 48 |
| 46 my $opts = { $this->options }; | 49 my $opts = { %{ $this->options } }; |
| 47 | 50 |
| 48 $opts->{STASH} = $this->context->stash->clone(); | 51 $opts->{STASH} = $this->context->stash->clone(); |
| 49 $opts->{LOAD_TEMPLATES} = $this->provider; | 52 $opts->{LOAD_TEMPLATES} = $this->provider; |
| 50 | 53 |
| 51 return new IMPL::Web::View::TTDocument( $tt, $opts, loader => $this ); | 54 return new IMPL::Web::View::TTDocument( $tt, $opts, loader => $this ); |
| 60 | 63 |
| 61 $name = $this->_appendExt($name); | 64 $name = $this->_appendExt($name); |
| 62 | 65 |
| 63 my ($tt,$error) = $this->provider->fetch($name); | 66 my ($tt,$error) = $this->provider->fetch($name); |
| 64 | 67 |
| 65 if ($error == STATUS_DECLINED) { | 68 if (defined $error and $error == STATUS_DECLINED) { |
| 66 die new IMPL::KeyNotFoundException($name); | 69 die new IMPL::KeyNotFoundException($name); |
| 67 } elsif ($error == STATUS_ERROR) { | 70 } elsif (defined $error and $error == STATUS_ERROR) { |
| 68 die new IMPL::Exception("Failed to load a template", $name, $tt); | 71 die new IMPL::Exception("Failed to load a template", $name, $tt); |
| 69 } | 72 } |
| 70 | 73 |
| 71 return $tt; | 74 return $tt; |
| 72 } | 75 } |
| 73 | 76 |
| 74 sub _appendExt { | 77 sub _appendExt { |
| 75 my ($this,$name) = @_; | 78 my ($this,$name) = @_; |
| 76 | |
| 77 | 79 |
| 78 if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) { | 80 if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) { |
| 79 return $name; | 81 return $name; |
| 80 } else { | 82 } else { |
| 81 return $name . $this->ext; | 83 return $name . $this->ext; |
| 84 | 86 |
| 85 sub _init { | 87 sub _init { |
| 86 my ($this) = @_; | 88 my ($this) = @_; |
| 87 | 89 |
| 88 if (!$this->isInitialized) { | 90 if (!$this->isInitialized) { |
| 89 $this->isInitialized(1); | |
| 90 | |
| 91 if ($this->initializer) { | 91 if ($this->initializer) { |
| 92 eval { | 92 eval { |
| 93 $this->context->process($this->initializer); | 93 $this->context->process($this->initializer); |
| 94 }; | |
| 95 if (my $e = $@) { | |
| 96 die new IMPL::Exception("Failed to process an initializer", $this->initializer, $e); | |
| 94 } | 97 } |
| 95 } | 98 } |
| 99 $this->isInitialized(1); | |
| 96 } | 100 } |
| 97 } | 101 } |
| 98 | 102 |
| 99 1; | 103 1; |
| 100 | 104 |
