# HG changeset patch # User cin # Date 1380891994 -14400 # Node ID 86b470004d4703d78556ed87282faeb7fbd1f254 # Parent f116cd9fe7d9d1fdf031a12bf36df9566f3411d7 added lables loading diff -r f116cd9fe7d9 -r 86b470004d47 Lib/IMPL/Web/View/TTContext.pm --- 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__ diff -r f116cd9fe7d9 -r 86b470004d47 _test/Resources/view/layout/default.tt --- /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 diff -r f116cd9fe7d9 -r 86b470004d47 _test/Resources/view/layout/footer.tt diff -r f116cd9fe7d9 -r 86b470004d47 _test/Resources/view/layout/header.tt diff -r f116cd9fe7d9 -r 86b470004d47 _test/Resources/view/layout/menu.tt diff -r f116cd9fe7d9 -r 86b470004d47 _test/Resources/view/layout/nav.tt diff -r f116cd9fe7d9 -r 86b470004d47 _test/Resources/view/packages/templates/ARRAY.tt --- 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 diff -r f116cd9fe7d9 -r 86b470004d47 _test/Resources/view/packages/templates/plain.tt --- 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 @@ -$model \ No newline at end of file +$model \ No newline at end of file diff -r f116cd9fe7d9 -r 86b470004d47 _test/Resources/view/site/product/view.tt --- 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 @@ -

PageTitle

+

$PageTitle

name: [% display('name') %]
description: [% display(model) %]
\ No newline at end of file diff -r f116cd9fe7d9 -r 86b470004d47 _test/temp.pl --- 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',{