Mercurial > pub > Impl
annotate Lib/IMPL/Web/View/TTLoader.pm @ 199:e743a8481327
Added REST support for forms (with only get and post methods)
| author | sergey |
|---|---|
| date | Mon, 23 Apr 2012 01:36:52 +0400 |
| parents | a705e848dcc7 |
| children | 891c04080658 |
| rev | line source |
|---|---|
| 181 | 1 package IMPL::Web::View::TTLoader; |
| 2 use strict; | |
| 3 | |
| 4 use IMPL::lang qw(:declare :constants); | |
| 5 | |
| 6 use Template::Provider(); | |
| 7 use Template::Context(); | |
| 8 use Template::Constants qw(:status); | |
| 9 | |
| 10 use IMPL::Web::View::TTDocument(); | |
| 11 | |
| 12 use parent qw( | |
| 194 | 13 IMPL::Object |
| 196 | 14 IMPL::Object::Serializable |
| 181 | 15 ); |
| 16 | |
| 17 BEGIN { | |
| 194 | 18 public property options => PROP_ALL; |
| 19 public property provider => PROP_GET | PROP_OWNERSET; | |
| 20 public property context => PROP_GET | PROP_OWNERSET; | |
| 21 public property ext => PROP_ALL; | |
|
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
22 public property layoutBase => PROP_GET | PROP_OWNERSET; |
| 194 | 23 |
| 24 public property isInitialized => PROP_GET | PROP_OWNERSET; | |
| 25 public property initializer => PROP_GET | PROP_OWNERSET; | |
| 26 | |
| 27 private property _globals => PROP_ALL; | |
| 181 | 28 } |
| 29 | |
| 196 | 30 sub save { |
| 31 my ($this,$context) = @_; | |
| 32 | |
| 33 $context->AddVar($_, $this->$_()) for qw(options provider context ext layoutBase); | |
| 34 } | |
| 35 | |
| 36 sub restore { | |
| 37 my ($class,$data,$surrogate) = @_; | |
| 38 | |
| 39 my %params = @$data; | |
| 40 | |
| 41 my $refOpts = delete $params{options}; | |
| 42 | |
| 43 if ($surrogate){ | |
| 44 $surrogate->callCTOR($refOpts,%params); | |
| 45 } else { | |
| 46 $surrogate = $class->new($refOpts,%params); | |
| 47 } | |
| 48 } | |
| 49 | |
| 181 | 50 sub CTOR { |
| 194 | 51 my ($this,$refOpts,%args) = @_; |
| 52 | |
| 53 $refOpts ||= {}; | |
| 54 | |
| 55 $this->ext($args{ext}) if $args{ext}; | |
| 56 $this->initializer($args{initializer}) if $args{initializer}; | |
| 57 $this->_globals(ref $args{globals} eq 'HASH' ? $args{globals} : {}); | |
| 58 | |
| 59 $this->options($refOpts); | |
|
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
60 $this->layoutBase($args{layoutBase}) if $args{layoutBase}; |
| 194 | 61 |
| 62 # to aviod cyclic references we need to do a copy of $refOpts | |
| 63 $refOpts->{LOAD_TEMPLATES} = $this->provider(new Template::Provider( { %$refOpts } )); | |
| 64 | |
| 65 $this->context(new Template::Context($refOpts)); | |
| 181 | 66 } |
| 67 | |
| 68 sub document { | |
| 194 | 69 my ($this,$name,$vars) = @_; |
| 70 | |
| 71 my $tt = $this->template($name); | |
| 72 | |
| 73 $this->_init(); | |
| 74 | |
| 75 my $opts = { %{ $this->options } }; | |
| 76 | |
| 77 $opts->{STASH} = $this->context->stash->clone(); | |
| 78 $opts->{LOAD_TEMPLATES} = $this->provider; | |
| 79 | |
| 80 return new IMPL::Web::View::TTDocument( $tt, $opts, $this, $vars ); | |
| 181 | 81 } |
| 82 | |
| 83 sub template { | |
| 194 | 84 my ($this,$name) = @_; |
| 85 | |
| 86 $name =~ s/^\s+|\s+$//g; | |
| 87 | |
| 88 die new IMPL::ArgumentException("A valid template name is required") unless length $name; | |
| 89 | |
| 90 $name = $this->_appendExt($name); | |
| 91 | |
| 92 my ($tt,$error) = $this->provider->fetch($name); | |
| 93 | |
| 94 if (defined $error and $error == STATUS_DECLINED) { | |
| 95 die new IMPL::KeyNotFoundException($name); | |
| 96 } elsif (defined $error and $error == STATUS_ERROR) { | |
| 97 die new IMPL::Exception("Failed to load a template", $name, $tt); | |
| 98 } | |
| 99 | |
| 100 return $tt; | |
| 181 | 101 } |
| 102 | |
|
195
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
103 sub layout { |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
104 my ($this,$name) = @_; |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
105 |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
106 my $layout; |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
107 |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
108 if ($this->layoutBase) { |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
109 $layout = $this->layoutBase . "/"; |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
110 } |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
111 |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
112 $layout .= $name; |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
113 return $this->template($layout); |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
114 } |
|
7a920771fd8e
IMPL::Web::View changed document layout handling, docs, examples
cin
parents:
194
diff
changeset
|
115 |
| 181 | 116 sub _appendExt { |
| 194 | 117 my ($this,$name) = @_; |
| 118 | |
| 119 return $name unless $this->ext; | |
| 120 | |
| 121 if (length $this->ext and substr( $name, -length($this->ext) ) eq $this->ext) { | |
| 122 return $name; | |
| 123 } else { | |
| 124 return $name . $this->ext; | |
| 125 } | |
| 181 | 126 } |
| 127 | |
| 128 sub _init { | |
| 194 | 129 my ($this) = @_; |
| 130 | |
| 131 if (!$this->isInitialized) { | |
| 132 my $initializer = $this->initializer || sub {}; | |
| 133 | |
| 134 eval { | |
| 135 $this->context->process($initializer,$this->_globals); | |
| 136 }; | |
| 137 if (my $e = $@) { | |
| 138 die new IMPL::Exception("Failed to process an initializer", $this->initializer, $e); | |
| 139 } | |
| 140 | |
| 141 $this->isInitialized(1); | |
| 142 } | |
| 181 | 143 } |
| 144 | |
| 145 1; | |
| 146 | |
| 147 __END__ | |
| 148 | |
| 149 =pod | |
| 150 | |
| 151 =head1 NAME | |
| 152 | |
| 153 C<IMPL::Web::View::TTLoader> - предоставляет глобальный контекст для загрузки шаблонов | |
| 154 | |
| 155 =head1 SYNOPSIS | |
| 156 | |
| 157 =begin code | |
| 158 | |
| 159 use IMPL::Web::View::TTLoader(); | |
| 160 | |
| 161 my $loader = new IMPL::Web::View::TTLoader( | |
| 194 | 162 { |
| 163 INCLUDE_PATH => [ | |
| 164 '/my/app/tt', | |
| 165 '/my/app/tt/lib' | |
| 166 ] | |
| 167 }, | |
| 168 ext => '.tt', | |
| 169 initializer => 'shared/global' | |
| 170 | |
| 181 | 171 ); |
| 172 | |
| 173 my $doc = $loader->document('index'); | |
| 174 | |
| 175 my $html = $doc->Render(); | |
| 176 | |
| 177 =end code | |
| 178 | |
| 179 =head1 DESCRIPTION | |
| 180 | |
| 182 | 181 =head1 MEMBERS |
| 182 | |
| 183 =head2 C<document($docName)> | |
| 184 | |
| 181 | 185 =cut |
| 186 |
