diff Lib/IMPL/Web/View/TTContext.pm @ 351:cfd7570c2af2

working on TTView: created TTView class for rendering models
author cin
date Tue, 08 Oct 2013 17:40:35 +0400
parents 86b470004d47
children 675cd1829255
line wrap: on
line diff
--- a/Lib/IMPL/Web/View/TTContext.pm	Mon Oct 07 17:35:54 2013 +0400
+++ b/Lib/IMPL/Web/View/TTContext.pm	Tue Oct 08 17:40:35 2013 +0400
@@ -31,6 +31,7 @@
 	   prefix
 	   cache
 	   includes
+	   modules
 	)) {
 		my $t = $prop;
 		
@@ -66,6 +67,7 @@
 	my ($this,$name) = @_;
 	
 	my $cache = $this->tt_cache;
+	
 	$this->tt_cache($cache = {}) unless $cache;
 	
 	if(my $tpl = $cache->{$name}) {
@@ -77,7 +79,7 @@
 	my $ext = $this->tt_ext || "";
 	
 	my $file;
-    
+	
 	foreach my $dir (@inc) {
 		$file = $dir ? "$dir/$name" : $name;
 		
@@ -85,15 +87,24 @@
 		
 		$file =  $ext ? "$file.$ext" : $file;
 		
-		warn "lookup: $file";
-		
-		my $tt = eval { $this->template($file) };
-		
-		return $cache->{$name} = {
-			base => $base,
-			labels => $this->load_labels($file),
-			template => $tt,
-		} if $tt;
+		if (exists($this->modules->{$file})) {
+			my $info = $this->modules->{$file};
+			return $cache->{$name} = $info
+                if $info;
+		} else {
+            if( my $tt = eval { $this->template($file) } ) {
+                my $info = {
+		            base => $base,
+		            labels => $this->load_labels($file),
+		            template => $tt,
+		            initialized => 0
+		        };
+		        $this->modules->{$file} = $info;
+		        return $cache->{$name} = $info;
+            } else {
+            	$this->modules->{$file} = undef;
+            }
+		}
 	}
 	
 	$this->throw(Template::Constants::ERROR_FILE, "$name: not found");
@@ -113,9 +124,7 @@
 	
 	my $prefix = $this->prefix;
 	
-	warn "no resolve" if $args and $args->{_no_resolve};
-	
-	if (not(($args and delete $args->{_no_resolve}) or ref $model)) {
+    if (not(($args and delete $args->{_no_resolve}) or ref $model)) {
 		$prefix = $prefix ? "${prefix}.${model}" : $model;
 		$model = $this->resolve_model($model);
 	} else {
@@ -136,6 +145,40 @@
     );
 }
 
+sub display_model {
+	my $this = shift;
+    my $model = shift;
+    my ($template, $args);
+    
+    if (ref $_[0] eq 'HASH') {
+        $args = shift;
+    } else {
+        $template = shift;
+        $args = shift;
+    }
+    
+    $args ||= {};
+    
+    my $prefix = delete $args->{prefix} || $this->prefix;
+    
+    if (my $rel = delete $args->{rel}) {
+    	$prefix = $prefix ? "${prefix}.${rel}" : $rel;
+    }
+    
+    $template = $template ? $this->find_template($template) : $this->find_template_for($model);
+    
+    return $this->render(
+        $template,
+        hashApply(
+            {
+                prefix => $prefix,
+                model => $model,
+            },
+            $args
+        )
+    );
+}
+
 sub invoke_environment {
 	my ($this,$code,$env) = @_;
 	
@@ -146,13 +189,18 @@
             hashApply(
 	            {
 					root => $this->root || $this,
+					modules => $this->modules || {},
 					cache => TypeKeyedCollection->new(),
 		            display => sub {
 		                $this->display(@_);
 		            },
 		            render => sub {
 		            	$this->render(@_);
-		            }
+		            },
+		            display_model => sub {
+		            	$this->display_model(@_);
+		            },
+		            tt_cache => {}
 				},
                 $env
             )
@@ -174,29 +222,38 @@
 	
 	$args ||= {};
 	
-	#TODO handle classes
-	
-	my ($base,$labels);
-	
-	$template = $this->find_template($template) unless ref $template;
+	my $info = ref $template ? $template : $this->find_template($template);
 	
-	if (ref $template eq 'HASH') {
-        $base = $template->{base};
-        $labels = $template->{labels};
-        $template = $template->{template};
-    } else {
-        carp "got an invalid template object: $template";
-        $base = $this->base;
+	if (ref($info) ne 'HASH') {
+		carp "got an invalid template object: $info (" . ref($info) . ")";
+    	$info = {
+    		template => $info,
+    		base => $this->base,
+    		initialized => 1
+    	};
     }
 	
-	return $this->invoke_environment(
-	   sub {
-	       return shift->include($template,$args);
+    return $this->invoke_environment(
+        sub {
+       	    my $ctx = shift;
+       	   
+       	    unless($info->{initialized}) {
+       	        if(my $init = $info->{template}->blocks->{INIT}) {
+       	        	$info->{initialized} = 1;
+       	            eval {
+       	                $ctx->visit($info->{template}->blocks);
+       	                $ctx->include($init);
+       	            };
+       	            $ctx->leave();
+       	        }
+       	    }
+       	   
+            return $ctx->include($info->{template},$args);
 	   },
 	   hashMerge(
-	       $labels || {},
+	       $info->{labels} || {},
 		   {
-		   	base => $base,
+		   	base => $info->{base},
 		   	parent => $this
 		   }
 	   )