diff Lib/IMPL/Web/View/TTDocument.pm @ 194:4d0e1962161c

Replaced tabs with spaces IMPL::Web::View - fixed document model, new features (control classes, document constructor parameters)
author cin
date Tue, 10 Apr 2012 20:08:29 +0400
parents 8e8401c0aea4
children 7a920771fd8e
line wrap: on
line diff
--- a/Lib/IMPL/Web/View/TTDocument.pm	Tue Apr 10 08:13:22 2012 +0400
+++ b/Lib/IMPL/Web/View/TTDocument.pm	Tue Apr 10 20:08:29 2012 +0400
@@ -10,127 +10,130 @@
 
 
 use parent qw(
-	IMPL::DOM::Document
-	IMPL::Web::View::TTControl
+    IMPL::DOM::Document
+    IMPL::Web::View::TTControl
 );
 
 BEGIN {
-	public _dom property layout => PROP_ALL;
-	public property opts => PROP_GET | PROP_OWNERSET;
-	public property loader => PROP_GET | PROP_OWNERSET;
-	public property controls => PROP_GET | PROP_OWNERSET;
-	
-	# store the stash separately to make require() method to work correctly
-	# even when a stash of the context is modified during the processing 
-	public property stash => PROP_GET | PROP_OWNERSET; 
+    public _dom property layout => PROP_ALL;
+    public property opts => PROP_GET | PROP_OWNERSET;
+    public property loader => PROP_ALL;
+    public property controls => PROP_GET | PROP_OWNERSET;
+    
+    # store the stash separately to make require() method to work correctly
+    # even when a stash of the context is modified during the processing 
+    public property stash => PROP_GET | PROP_OWNERSET; 
 }
 
 sub CTOR {
-	my ($this,$template,$refOpts,%args) = @_;
-	
-	$this->controls({});
-	$this->loader($args{loader}) if $args{loader};
-	
-	$this->layout( $template->layout ) unless $this->layout;
-	
-	$this->opts($refOpts);
-	$this->stash($this->context->stash);
-	
-	my $self = $this;
-	weaken($self);
-	
-	$this->templateVars('require', sub {
-		my $doc = $self;
-		die new IMPL::Exception("A document is destroyed or invalid") unless $doc;
-		$doc->require(@_);
-	});
-	
-	$this->templateVars('document', sub { $self } );
-	$this->InitInstance();
+    my ($this,$template,$refOpts,$loader,$vars) = @_;
+    
+    $this->controls({});
+    $this->loader($loader) if $loader;
+    
+    $this->layout( $template->layout ) unless $this->layout;
+    
+    $this->opts($refOpts);
+    $this->stash($this->context->stash);
+    
+    my $self = $this;
+    weaken($self);
+    
+    $this->templateVars('require', sub {
+        my $doc = $self;
+        die new IMPL::Exception("A document is destroyed or invalid") unless $doc;
+        $doc->require(@_);
+    });
+    
+    $this->templateVars('document', sub { $self } );
+    $this->InitInstance($vars);
 }
 
 our %CTOR = (
-	'IMPL::Web::View::TTControl' => sub {
-		'document',
-		$_[0], # template
-		new Template::Context($_[1])  # context
-	},
-	'IMPL::DOM::Document' => sub {
-		nodeName => 'document'
-	}
+    'IMPL::Web::View::TTControl' => sub {
+        my ($template,$contextOpts) = @_;
+        'document',
+        $_[0], # template
+        new Template::Context($_[1])  # context
+    },
+    'IMPL::DOM::Document' => sub {
+        nodeName => 'document'
+    }
 );
 
 sub templateVars {
-	my $this = shift;
-	my $name = shift;
-	
-	if (@_) {
-		return $this->stash->set($name, shift);		
-	} else {
-		return $this->stash->get($name);
-	}
+    my $this = shift;
+    my $name = shift;
+    
+    if (@_) {
+        return $this->stash->set($name, shift);        
+    } else {
+        return $this->stash->get($name);
+    }
 }
 
 sub require {
-	my ($this, $control, $nodeProps) = @_;
-	
-	$nodeProps ||= {};
-	$nodeProps->{document} = $this;
-	
-	if (my $factory = $this->controls->{$control}) {
-		return $factory;
-	} else {
-	
-		my $path = $control;
-		if ( my $template = $this->loader->template($path) ) {
+    my ($this, $control, $nodeProps) = @_;
+    
+    $nodeProps ||= {};
+    $nodeProps->{document} = $this;
+    
+    if (my $factory = $this->controls->{$control}) {
+        return $factory;
+    } else {
+    
+        my $path = $control;
+        if ( my $template = $this->loader->template($path) ) {
+            my $opts = { %{$this->opts} };
 
-			my $opts = { %{$this->opts} };
-			$opts->{STASH} = $this->context->stash->clone();
+            # avoid propagation of local variables 
+            $opts->{STASH} = $this->stash->clone();
  
-			my $ctx = new Template::Context($opts);
-			
-			$factory = new IMPL::Web::View::TTFactory(
-				typeof IMPL::Web::View::TTControl,
-				$template,
-				$ctx,
-				$opts,
-				{ document => $this }
-			);
-			
-			my @parts = split(/\/+/,$control);
-			
-			$this->controls->{$control} = $factory;
-						
-			return $factory;
+            my $ctx = new Template::Context($opts);
+            
+            $factory = new IMPL::Web::View::TTFactory(
+                $template->class || typeof IMPL::Web::View::TTControl,
+                $template,
+                $ctx,
+                $opts
+            );
+            
+            my @parts = split(/\/+/,$control);
+            
+            $this->controls->{$control} = $factory;
+                        
+            return $factory;
 
-		} else {
-			die new IMPL::KeyNotFoundException($control);
-		}
-	}
+        } else {
+            die new IMPL::KeyNotFoundException($control);
+        }
+    }
 }
 
 sub Render {
-	my ($this,$args) = @_;
-	
-	my $output;
-	
-	if ($this->layout) {
-		$output = $this->context->include(
-			$this->loader->template($this->layout),
-			{
-				content => sub { $output ||= $this->RenderContent($args); }
-			}
-		);
-	} else {
-		return $this->RenderContent($args);
-	}
-	
-	return $output;
+    my ($this,$args) = @_;
+    
+    my $output;
+    
+    if ($this->layout) {
+        $output = $this->context->include(
+            $this->loader->template($this->layout),
+            {
+                content => sub { $output ||= $this->RenderContent($args); },
+                this => $this,
+                template => $this->template
+            }
+        );
+    } else {
+        return $this->RenderContent($args);
+    }
+    
+    return $output;
 }
 
 sub RenderContent {
-	my $this = shift;
-	return $this->SUPER::Render(@_);
+    my $this = shift;
+    return $this->SUPER::Render(@_);
 }
 
 
@@ -215,7 +218,7 @@
 
 =over
 
-=item 1 C<require('my/org/input')>
+=item 1 C<TInput = require('my/org/input')>
 
 =item 1 Загружает шаблон C<my/org/input.tt>
 
@@ -229,7 +232,7 @@
 
 =over
 
-=item 1 C<< my.org.input.new('login') >>
+=item 1 C<< TInput.new('login') >>
 
 =item 1 Если это первый элемент управления, то выполняетя статический конструктор в контексте фабрики