changeset 349:86b470004d47

added lables loading
author cin
date Fri, 04 Oct 2013 17:06:34 +0400
parents f116cd9fe7d9
children f356c4894d1b
files Lib/IMPL/Web/View/TTContext.pm _test/Resources/view/layout/default.tt _test/Resources/view/layout/footer.tt _test/Resources/view/layout/header.tt _test/Resources/view/layout/menu.tt _test/Resources/view/layout/nav.tt _test/Resources/view/packages/templates/ARRAY.tt _test/Resources/view/packages/templates/plain.tt _test/Resources/view/site/product/view.tt _test/temp.pl
diffstat 6 files changed, 72 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/Lib/IMPL/Web/View/TTContext.pm	Thu Oct 03 19:48:57 2013 +0400
+++ b/Lib/IMPL/Web/View/TTContext.pm	Fri Oct 04 17:06:34 2013 +0400
@@ -2,14 +2,18 @@
 use strict;
 use Template::Base;
 use Carp qw(carp);
+use File::Spec();
+use IMPL::Resources::Format qw(FormatMessage);
+use IMPL::Resources::Strings();
 
 use IMPL::Exception();
-use IMPL::lang qw(is typeof hashApply);
+use IMPL::lang qw(is typeof hashApply hashMerge);
 use IMPL::declare {
 	require => {
 	   Document => '-Template::Document',
 	   TypeKeyedCollection => 'IMPL::TypeKeyedCollection',
-	   ArgException => "-IMPL::InvalidArgumentException"
+	   ArgException => '-IMPL::InvalidArgumentException',
+	   Resources => 'IMPL::Resources'
 	},
 	base => [
 		'Template::Context' => '@_'
@@ -87,6 +91,7 @@
 		
 		return $cache->{$name} = {
 			base => $base,
+			labels => $this->load_labels($file),
 			template => $tt,
 		} if $tt;
 	}
@@ -108,7 +113,9 @@
 	
 	my $prefix = $this->prefix;
 	
-	if (not(($args and delete $args->{'-no-resolve'}) or ref $model)) {
+	warn "no resolve" if $args and $args->{_no_resolve};
+	
+	if (not(($args and delete $args->{_no_resolve}) or ref $model)) {
 		$prefix = $prefix ? "${prefix}.${model}" : $model;
 		$model = $this->resolve_model($model);
 	} else {
@@ -169,12 +176,13 @@
 	
 	#TODO handle classes
 	
-	my $base;
+	my ($base,$labels);
 	
 	$template = $this->find_template($template) unless ref $template;
 	
 	if (ref $template eq 'HASH') {
         $base = $template->{base};
+        $labels = $template->{labels};
         $template = $template->{template};
     } else {
         carp "got an invalid template object: $template";
@@ -185,10 +193,13 @@
 	   sub {
 	       return shift->include($template,$args);
 	   },
-	   {
-	   	base => $base,
-	   	parent => $this
-	   }
+	   hashMerge(
+	       $labels || {},
+		   {
+		   	base => $base,
+		   	parent => $this
+		   }
+	   )
 	)
 }
 
@@ -240,6 +251,52 @@
 	return;
 }
 
+sub get_real_file {
+	my ($this,$fname) = @_;
+	
+	my @path = split(/\/+/,$fname);
+	
+	foreach my $provider (@{$this->load_templates || []}) {
+		foreach my $dir (@{$provider->paths || []}) {
+			my $realName = File::Spec->catfile($dir,@path);
+			return $realName if -f $realName; 
+		}
+	}
+}
+
+sub load_labels {
+    my ($this,$fname) = @_;
+    
+    $fname = $this->get_real_file($fname);
+    
+    my %vars;
+    
+    my $flabels = "$fname.labels";
+        
+    if (-f $flabels) {
+        
+        my %labels;
+        $labels{default} = IMPL::Resources::Strings::ParseStringsMap($flabels);
+        
+        while(my($label,$text) = each %{$labels{default}}) {
+            $vars{$label} = sub {
+                my ($params) = @_;
+                my $locale = Resources->currentLocale;
+            
+                unless ($labels{$locale}) {
+                $labels{$locale} = -f "$fname.$locale" ? 
+                    IMPL::Resources::Strings::ParseStringsMap("$fname.$locale") :
+                    {};
+                }
+                    
+                return FormatMessage(($labels{$locale}{$label} || $text),$params);
+            }
+        }
+    }
+    
+    return \%vars;
+}
+
 1;
 
 __END__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/_test/Resources/view/layout/default.tt	Fri Oct 04 17:06:34 2013 +0400
@@ -0,0 +1,4 @@
+[% render('header') %]
+[% render('menu') %]
+[% render('nav') %]
+[% render('footer') %]
\ No newline at end of file
--- a/_test/Resources/view/packages/templates/ARRAY.tt	Thu Oct 03 19:48:57 2013 +0400
+++ b/_test/Resources/view/packages/templates/ARRAY.tt	Fri Oct 04 17:06:34 2013 +0400
@@ -1,3 +1,3 @@
 [% FOR item IN model %]
-    * ${loop.index}. [% display(loop.index) %]
+    * ${loop.index}. [% display(item, { _no_resolve => 1, prefix => "$prefix[$loop.index]"}) %]
 [% END %]
\ No newline at end of file
--- a/_test/Resources/view/packages/templates/plain.tt	Thu Oct 03 19:48:57 2013 +0400
+++ b/_test/Resources/view/packages/templates/plain.tt	Fri Oct 04 17:06:34 2013 +0400
@@ -1,1 +1,1 @@
-<span prefix="$prefix">$model</span>
\ No newline at end of file
+<input name="$prefix">$model</input>
\ No newline at end of file
--- a/_test/Resources/view/site/product/view.tt	Thu Oct 03 19:48:57 2013 +0400
+++ b/_test/Resources/view/site/product/view.tt	Fri Oct 04 17:06:34 2013 +0400
@@ -1,3 +1,3 @@
-<h1> PageTitle </h1>
+<h1> $PageTitle </h1>
 <div>name: [% display('name') %]</div>
 <div>description: [% display(model) %]</div>
\ No newline at end of file
--- a/_test/temp.pl	Thu Oct 03 19:48:57 2013 +0400
+++ b/_test/temp.pl	Fri Oct 04 17:06:34 2013 +0400
@@ -11,8 +11,6 @@
 	RECURSION => 1000
 });
 
-warn $ctx->load_templates->[0]->include_path->[0];
-
 print $ctx->invoke_environment(sub {
 	return shift->render(
 	    'product/view',{