Mercurial > pub > Impl
annotate Lib/IMPL/Web/QueryHandler/PageFormat.pm @ 136:f6af119ac741
url routines for templates
author | wizard |
---|---|
date | Fri, 25 Jun 2010 16:45:56 +0400 |
parents | 44977efed303 |
children | 6975cd4973d1 |
rev | line source |
---|---|
62 | 1 package IMPL::Web::QueryHandler::PageFormat; |
64 | 2 use base qw(IMPL::Web::QueryHandler IMPL::Object::Autofill); |
136 | 3 use strict; |
62 | 4 |
5 __PACKAGE__->PassThroughArgs; | |
6 | |
7 use IMPL::Class::Property; | |
77 | 8 use IMPL::Web::TT::Document; |
136 | 9 use Template::Plugin::URL; |
97 | 10 use IMPL::Security::Context; |
65 | 11 use File::Spec; |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
12 use Error qw(:try); |
62 | 13 |
136 | 14 $Template::Plugin::URL::JOINT = '&'; |
15 | |
64 | 16 BEGIN { |
65 | 17 public property templatesCharset => prop_all; |
18 public property templatesBase => prop_all; | |
97 | 19 public property defaultTarget => prop_all; |
116 | 20 public property pathinfoPrefix => prop_all; |
134 | 21 public property cache => prop_all; |
64 | 22 } |
23 | |
24 sub CTOR { | |
25 my ($this) = @_; | |
26 | |
65 | 27 $this->templatesCharset('utf-8') unless $this->templatesCharset; |
134 | 28 $this->cache(File::Spec->rel2abs($this->cache)) if $this->cache; |
29 $this->templatesBase(File::Spec->rel2abs($this->templatesBase)) if $this->templatesBase; | |
64 | 30 } |
31 | |
62 | 32 sub Process { |
33 my ($this,$action,$nextHandler) = @_; | |
34 | |
134 | 35 my $doc = new IMPL::Web::TT::Document(cache => $this->cache); |
62 | 36 |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
37 try { |
97 | 38 |
39 $this->templatesBase($ENV{DOCUMENT_ROOT}) unless $this->templatesBase; | |
40 | |
136 | 41 my ($requestUri) = split /\?/, $ENV{REQUEST_URI}; |
42 | |
43 my $pathInfo; | |
44 | |
45 if ( $requestUri eq $ENV{SCRIPT_NAME}.$ENV{PATH_INFO} ) { | |
46 # CGI with path info | |
47 $pathInfo = $ENV{PATH_INFO}; | |
48 } else { | |
49 die "REQUEST_URI: $ENV{REQUEST_URI}\nPATH_INFO: $ENV{PATH_INFO}" unless $ENV{REQUEST_URI} eq $ENV{PATH_INFO}; | |
50 } | |
51 | |
52 my @root = (''); | |
53 my @base; | |
54 | |
116 | 55 if (my $rx = $this->pathinfoPrefix) { |
136 | 56 $requestUri =~ s/^($rx)//; |
57 push @root, grep $_, split /\//, $1 if $1; | |
116 | 58 } |
136 | 59 |
60 $pathInfo = $requestUri unless defined $pathInfo; | |
61 | |
62 @base = grep $_, split /\//, ($pathInfo ? substr $requestUri,0, -length($pathInfo) : $requestUri); | |
63 | |
116 | 64 local $ENV{PATH_INFO} = $pathInfo || $this->defaultTarget; |
97 | 65 |
127
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
66 my @path = grep $_, split /\//, ($ENV{PATH_INFO} || '') or die new IMPL::Exception("PATH_INFO is empty and no defaultTarget specified" ); |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
67 |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
68 my @pathContainer = @path; |
0dce0470a3d8
In the IMPL::Web::ControllerUnit added the ability to notify a form about a wrong data from a transaction
wizard
parents:
121
diff
changeset
|
69 pop @pathContainer; |
65 | 70 |
121 | 71 $doc->LoadFile ( File::Spec->catfile($this->templatesBase,@path), $this->templatesCharset, $this->templatesBase ); |
97 | 72 $doc->AddVar( result => $nextHandler->() ); |
129 | 73 $doc->AddVar( app => $action->application ); |
136 | 74 $doc->AddVar( absoluteUrl => sub { join '/', @root, $_[0] } ); |
75 $doc->AddVar( baseUrl => sub { join '/', @root, @base, $_[0] } ); | |
76 $doc->AddVar( relativeUrl => sub { join '/', @root, @base, @pathContainer,$_[0] } ); | |
97 | 77 { |
78 local $@; | |
116 | 79 $doc->AddVar( user => IMPL::Security::Context->current->principal ); |
80 $doc->AddVar( session => IMPL::Security::Context->current ); | |
97 | 81 warn $@ if $@; |
82 } | |
63
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
83 |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
84 $action->response->contentType('text/html'); |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
85 my $hOut = $action->response->streamBody; |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
86 print $hOut $doc->Render(); |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
87 } finally { |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
88 $doc->Dispose; |
76b878ad6596
Added serialization support for the IMPL::Object::List
wizard
parents:
62
diff
changeset
|
89 }; |
62 | 90 } |
91 | |
65 | 92 1; |
93 | |
94 __END__ | |
95 | |
96 =pod | |
97 | |
98 =head1 NAME | |
99 | |
100 C<IMPL::Web::QueryHandler::PageFormat> - Выдача результатов в виде HTML страницы, построенной из шаблона. | |
101 | |
102 =head1 SYNOPSIS | |
103 | |
104 В файле конфигурации приложения | |
105 | |
106 =begin code xml | |
107 | |
108 <handlersQuery type="IMPL::Object::List"> | |
109 <item type="IMPL::Web::QueryHandler::PageFormat"> | |
110 <charsetTemplates>utf-8</charsetTemplates> | |
111 </item> | |
112 </handlersQuery> | |
113 | |
114 =end code xml | |
115 | |
116 Программно | |
117 | |
118 =begin code | |
119 | |
120 my $app = new IMPL::Web::Application(); | |
121 $app->handlersQuery->Add( | |
122 new IMPL::Web::QueryHandler::PageFormat( charsetTemplates=> 'utf-8' ); | |
123 ); | |
124 | |
125 =end | |
126 | |
127 =head1 DESCRIPTION | |
128 | |
129 Обработчик запроса для веб приложения. Загружает шаблон, путь к котрому берется | |
130 из C<ENV{PATH_INFO}> относительно пути из свойства C<templatesBase>. | |
131 | |
132 Наследуется от C<IMPL::Web::QueryHandler> для реализации функционала | |
133 обработчика запроса и переопределяет метод C<Process>. | |
134 | |
135 C<Serializable> | |
136 | |
137 =head1 MEMBERS | |
138 | |
139 =over | |
140 | |
141 =item C<CTOR(%props)> | |
142 | |
143 Создает новый экземпляр и заполняет свойства. | |
144 | |
145 =item C<[get,set] templatesCharset> | |
146 | |
147 Кодировка шаблонов. По умолчанию utf-8. | |
148 | |
149 =item C<[get,set] templatesBase> | |
150 | |
151 Каталог относительно которого ищется шаблон. | |
152 | |
153 =item C<[override] Process($action,$nextHandler)> | |
154 | |
155 Метод, переопределяющий C<IMPL::Web::QueryHandler->Process> и которому передается управление | |
156 для выполнения действий. | |
157 | |
158 =back | |
159 | |
160 =cut |