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

Улучшены тесты Исправлены ошибки Улучшена документация Работа над схемой DOM
author Sergey
date Mon, 24 Aug 2009 01:05:34 +0400
parents e2cd73ccc5bd
children 16ada169ca75
line wrap: on
line diff
--- a/Lib/IMPL/Web/TDocument.pm	Fri Aug 14 16:14:13 2009 +0400
+++ b/Lib/IMPL/Web/TDocument.pm	Mon Aug 24 01:05:34 2009 +0400
@@ -2,26 +2,144 @@
 use strict;
 use warnings;
 
-use base qw(IMPL::DOM::Node);
+use base qw(IMPL::DOM::Node IMPL::Object::Disposable);
 use Template::Context;
 use Template::Provider;
 use IMPL::Class::Property;
 use File::Spec;
 
 BEGIN {
-    public property Templates => prop_get | owner_set;
-    public property Context => prop_get | owner_set;
+    private property _Provider => prop_all;
+    private property _Context => prop_all;
+    public property Template => prop_get | owner_set;
 }
 
 our %CTOR = (
     'IMPL::DOM::Node' => sub { nodeName => 'document' }
 );
 
-sub load {
-    my ($this,$file) = @_;
+sub Provider {
+    my ($this,%args) = @_;
+    
+    if (my $provider = $this->_Provider) {
+        return $provider;
+    } else {
+        return $this->_Provider(new Template::Provider(
+            \%args
+        ));
+    }
+}
+
+sub Context {
+    my ($this) = @_;
+    
+    if (my $ctx = $this->_Context) {
+        return $ctx;
+    } else {
+        return $this->_Context (
+            new Template::Context(
+                VARIABLES => {
+                    document => $this
+                },
+                TRIM => 1,
+                RECURSION => 1,
+                LOAD_TEMPLATES => [$this->Provider]
+            )
+        )
+    }
+}
+
+sub loadFile {
+    my ($this,$filePath,$encoding) = @_;
+    
+    die new IMPL::InvalidArgumentException("A filePath parameter is required") unless $filePath;
     
-    $file = File::Spec->rel2abs($file);
+    $encoding ||= 'utf8';
+    
+    $this->_Context(undef);
+    $this->_Provider(undef);
+    
+    my ($vol,$dir,$fileName) = File::Spec->splitpath($filePath);
+    
+    my $inc = File::Spec->catpath($vol,$dir,'');
+    
+    $this->Provider(
+        ENCODING => $encoding,
+        INTERPOLATE => 1,
+        PRE_CHOMP => 1,
+        POST_CHOMP => 1,
+        INCLUDE_PATH => $inc
+    );
     
+    $this->Template($this->Context->template($fileName));
+}
+
+sub Title {
+    $_[0]->Template->Title;
+}
+
+sub Render {
+    my ($this) = @_;
+    
+    return $this->Template->process($this->Context);
+}
+
+sub Dispose {
+    my ($this) = @_;
+    
+    $this->Template(undef);
+    $this->_Context(undef);
+    $this->_Provider(undef);
+    
+    $this->SUPER::Dispose();
 }
 
 1;
+__END__
+=pod
+
+=head1 SYNOPSIS
+
+// create new document
+my $doc = new IMPL::Web::TDocument;
+
+// load template
+$doc->loadFile('Templates/index.tt');
+
+// render file
+print $doc->Render();
+
+=head1 DESCRIPTION
+
+,    Template::Toolkit.   ,
+   .   C<IMPL::DOM::Node>,
+..      DOM .
+
+   C<document>    .  
+        , 
+   C<Dispose>   .
+
+=head1 METHODS
+
+=level 4
+
+=item C<new()>
+
+   
+
+=item C<$doc->loadFile($fileName,$encoding)>
+
+    C<$fileName>,   C<$encoding>. 
+  ,  utf-8.
+
+=item C<$doc->Render()>
+
+      .
+
+=item C<$doc->Dispose()>
+
+      .
+
+=back
+
+=cut
\ No newline at end of file