comparison Lib/IMPL/Web/TDocument.pm @ 7:94d47b388442

Улучшены тесты Исправлены ошибки Улучшена документация Работа над схемой DOM
author Sergey
date Mon, 24 Aug 2009 01:05:34 +0400
parents e2cd73ccc5bd
children 16ada169ca75
comparison
equal deleted inserted replaced
6:e2cd73ccc5bd 7:94d47b388442
1 package IMPL::Web::TDocument; 1 package IMPL::Web::TDocument;
2 use strict; 2 use strict;
3 use warnings; 3 use warnings;
4 4
5 use base qw(IMPL::DOM::Node); 5 use base qw(IMPL::DOM::Node IMPL::Object::Disposable);
6 use Template::Context; 6 use Template::Context;
7 use Template::Provider; 7 use Template::Provider;
8 use IMPL::Class::Property; 8 use IMPL::Class::Property;
9 use File::Spec; 9 use File::Spec;
10 10
11 BEGIN { 11 BEGIN {
12 public property Templates => prop_get | owner_set; 12 private property _Provider => prop_all;
13 public property Context => prop_get | owner_set; 13 private property _Context => prop_all;
14 public property Template => prop_get | owner_set;
14 } 15 }
15 16
16 our %CTOR = ( 17 our %CTOR = (
17 'IMPL::DOM::Node' => sub { nodeName => 'document' } 18 'IMPL::DOM::Node' => sub { nodeName => 'document' }
18 ); 19 );
19 20
20 sub load { 21 sub Provider {
21 my ($this,$file) = @_; 22 my ($this,%args) = @_;
22 23
23 $file = File::Spec->rel2abs($file); 24 if (my $provider = $this->_Provider) {
25 return $provider;
26 } else {
27 return $this->_Provider(new Template::Provider(
28 \%args
29 ));
30 }
31 }
32
33 sub Context {
34 my ($this) = @_;
24 35
36 if (my $ctx = $this->_Context) {
37 return $ctx;
38 } else {
39 return $this->_Context (
40 new Template::Context(
41 VARIABLES => {
42 document => $this
43 },
44 TRIM => 1,
45 RECURSION => 1,
46 LOAD_TEMPLATES => [$this->Provider]
47 )
48 )
49 }
50 }
51
52 sub loadFile {
53 my ($this,$filePath,$encoding) = @_;
54
55 die new IMPL::InvalidArgumentException("A filePath parameter is required") unless $filePath;
56
57 $encoding ||= 'utf8';
58
59 $this->_Context(undef);
60 $this->_Provider(undef);
61
62 my ($vol,$dir,$fileName) = File::Spec->splitpath($filePath);
63
64 my $inc = File::Spec->catpath($vol,$dir,'');
65
66 $this->Provider(
67 ENCODING => $encoding,
68 INTERPOLATE => 1,
69 PRE_CHOMP => 1,
70 POST_CHOMP => 1,
71 INCLUDE_PATH => $inc
72 );
73
74 $this->Template($this->Context->template($fileName));
75 }
76
77 sub Title {
78 $_[0]->Template->Title;
79 }
80
81 sub Render {
82 my ($this) = @_;
83
84 return $this->Template->process($this->Context);
85 }
86
87 sub Dispose {
88 my ($this) = @_;
89
90 $this->Template(undef);
91 $this->_Context(undef);
92 $this->_Provider(undef);
93
94 $this->SUPER::Dispose();
25 } 95 }
26 96
27 1; 97 1;
98 __END__
99 =pod
100
101 =head1 SYNOPSIS
102
103 // create new document
104 my $doc = new IMPL::Web::TDocument;
105
106 // load template
107 $doc->loadFile('Templates/index.tt');
108
109 // render file
110 print $doc->Render();
111
112 =head1 DESCRIPTION
113
114 , Template::Toolkit. ,
115 . C<IMPL::DOM::Node>,
116 .. DOM .
117
118 C<document> .
119 ,
120 C<Dispose> .
121
122 =head1 METHODS
123
124 =level 4
125
126 =item C<new()>
127
128
129
130 =item C<$doc->loadFile($fileName,$encoding)>
131
132 C<$fileName>, C<$encoding>.
133 , utf-8.
134
135 =item C<$doc->Render()>
136
137 .
138
139 =item C<$doc->Dispose()>
140
141 .
142
143 =back
144
145 =cut