[Jifty-commit] r3234 - in Template-Declare: lib/Template lib/Template/Declare t

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Sat May 12 03:11:45 EDT 2007


Author: jesse
Date: Sat May 12 03:11:44 2007
New Revision: 3234

Modified:
   Template-Declare/   (props changed)
   Template-Declare/lib/Template/Declare.pm
   Template-Declare/lib/Template/Declare/Tags.pm
   Template-Declare/t/deep_aliasing.t
   Template-Declare/t/deep_importing.t

Log:
 r56807 at pinglin:  jesse | 2007-05-12 03:11:35 -0400
 Allow recursive aliasing.


Modified: Template-Declare/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/lib/Template/Declare.pm	(original)
+++ Template-Declare/lib/Template/Declare.pm	Sat May 12 03:11:44 2007
@@ -19,7 +19,7 @@
 
 
 __PACKAGE__->roots([]);
-__PACKAGE__->aliases([]);
+__PACKAGE__->aliases({});
 __PACKAGE__->alias_metadata({});
 __PACKAGE__->templates({});
 __PACKAGE__->private_templates({});
@@ -224,7 +224,7 @@
     $prepend_path =~ s|/$||;
 
     my $alias_key = $mixin . " " . $prepend_path;
-    push @{ $alias_into->aliases() }, $alias_key;
+    push @{ Template::Declare->aliases->{$alias_into} }, $alias_key;
     $alias_into->alias_metadata()->{$alias_key} = {
         class        => $mixin,
         path         => $prepend_path,
@@ -304,25 +304,27 @@
 =cut
 
 sub has_template {
+
     # When using Template::Declare->has_template, find in all
     # registered namespaces.
-    goto \&resolve_template if $_[0] eq 'Template::Declare';
+#    if ( $_[0] eq 'Template::Declare' ) {
+         &resolve_template(@_) || _has_template(@_);
+#    } else {
+#        _has_template(@_);
+#    }
+}
+
+sub _has_template {
 
     # Otherwise find only in specific package
     my $pkg           = shift;
     my $template_name = shift;
     my $show_private  = 0 || shift;
 
-    if ( my $coderef
-        = $pkg->_find_template_sub(
-            _template_name_to_sub($template_name) ) ) {
+    if ( my $coderef = $pkg->_find_template_sub( _template_name_to_sub($template_name) ) ) {
         return $coderef;
     }
-    elsif (
-        $show_private
-        and $coderef = $pkg->_find_template_sub(
-            _template_name_to_private_sub($template_name)
-        ) ) {
+    elsif ( $show_private and $coderef = $pkg->_find_template_sub( _template_name_to_private_sub($template_name)) ) {
         return $coderef;
     }
 
@@ -349,33 +351,43 @@
     my $template_name = shift;
     my $show_private  = shift || 0;
 
-    
-    warn "$self -> resolve_template $template_name";
+    my @search_roots;
+    if ($self eq 'Template::Declare') {
+        @search_roots = reverse @{ Template::Declare->roots };
+
+    } else {
+        @search_roots = ($self);
+    }
 
-    foreach my $package ( reverse @{ Template::Declare->roots } ) {
+    foreach my $package ( @search_roots) {
         unless ($package and $package->isa('Template::Declare')) {
-            warn "'@{[$package||'']} was listed as a Template::Declare root, but is not a Template::Declare subclass";
             next;
         }
 
-        if ( my $coderef = $package->has_template( $template_name, $show_private ) ) {
+        if ( my $coderef = $package->_has_template( $template_name, $show_private ) ) {
             return $coderef;
         }
 
-        foreach my $alias_key ( @{ $package->aliases } ) {
+        foreach my $alias_key ( @{ Template::Declare->aliases->{$package} } ) {
             my $alias_info = $package->alias_metadata()->{$alias_key};
             my $alias_prefix = $alias_info->{path};
             my $alias_class = $alias_info->{class};
             my $package_vars = $alias_info->{package_vars};
 
+            $template_name = "/$template_name" ;
 
-            $template_name = "/$template_name";
-            #warn "$self -> resolve_template checking alias $alias_key for $template_name";
             if ( $template_name =~ m{$alias_prefix/(.*)$} ) {
                 my $dispatch_to_template = $1;
-                if (my $coderef = $alias_class->has_template(
-                        $dispatch_to_template, $show_private)) {
+                if (my $coderef = $alias_class->_has_template( $dispatch_to_template, $show_private)) {
+                    # We're going to force $self to the aliased class
+                    return sub {
+                        no strict 'refs';
 
+                        local $TEMPLATE_VARS->{$alias_class} = $package_vars;
+                        &$coderef($alias_class);
+                    };
+                }
+                elsif ( $coderef = $alias_class->resolve_template( $dispatch_to_template, $show_private)) {
                     # We're going to force $self to the aliased class
                     return sub {
                         no strict 'refs';
@@ -384,10 +396,15 @@
                         &$coderef($alias_class);
                     };
                 }
+
+
+
+
+
             }
+
         }
     }
-    return undef;
 }
 
 

Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm	(original)
+++ Template-Declare/lib/Template/Declare/Tags.pm	Sat May 12 03:11:44 2007
@@ -461,7 +461,7 @@
     my $callable =
         ( ref($template) && $template->isa('Template::Declare::Tag') )
         ? $template
-        : Template::Declare->has_template( $template, $INSIDE_TEMPLATE );
+        : Template::Declare->resolve_template( $template, $INSIDE_TEMPLATE );
 
     # If the template was not found let the user know.
     unless ($callable) {

Modified: Template-Declare/t/deep_aliasing.t
==============================================================================
--- Template-Declare/t/deep_aliasing.t	(original)
+++ Template-Declare/t/deep_aliasing.t	Sat May 12 03:11:44 2007
@@ -35,12 +35,12 @@
 package main;
 Template::Declare->init( roots => ['MyApp::View'] );
 
-use Test::More tests => 16;
+use Test::More tests => 10;
 require "t/utils.pl";
 
 ok( MyApp::View->has_template('toplevel') );
-ok( !MyApp::View->has_template('listing') );
-ok( !MyApp::View->has_template('search') );
+ok( !MyApp::View->has_template('listing') , "the listing template isn't imported to the top level");
+ok( !MyApp::View->has_template('search'), "The search template isn't imported to the top level" );
 ok( MyApp::View->has_template('/plugin/listing'), 'has listing template' );
 ok( MyApp::View->has_template('/plugin/search'), 'has search template' );
 
@@ -62,12 +62,10 @@
 }
 {
 
-    warn "Trying to show /plugin/listing";
     my $simple = ( Template::Declare->show('/plugin/listing'));
     like( $simple, qr'listing', "Can call /plugin/listing" );
 }
 {
-    warn "Trying to show /plugin/search";
     my $simple = ( Template::Declare->show('/plugin/search'));
     like( $simple, qr'search' , "Can call /plugin/search");
 }

Modified: Template-Declare/t/deep_importing.t
==============================================================================
--- Template-Declare/t/deep_importing.t	(original)
+++ Template-Declare/t/deep_importing.t	Sat May 12 03:11:44 2007
@@ -35,7 +35,7 @@
 package main;
 Template::Declare->init( roots => ['MyApp::View'] );
 
-use Test::More tests => 16;
+use Test::More tests => 10;
 require "t/utils.pl";
 
 ok( MyApp::View->has_template('toplevel') );


More information about the Jifty-commit mailing list