[Jifty-commit] r2364 - in Template-Declare: lib/Template lib/Template/Declare

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Sun Dec 10 21:15:34 EST 2006


Author: jesse
Date: Sun Dec 10 21:15:34 2006
New Revision: 2364

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

Log:
 r46080 at pinglin:  jesse | 2006-12-11 02:06:21 +0000
  * private templates


Modified: Template-Declare/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/lib/Template/Declare.pm	(original)
+++ Template-Declare/lib/Template/Declare.pm	Sun Dec 10 21:15:34 2006
@@ -31,27 +31,65 @@
 sub has_template {
     my $pkg = shift;
     my $template_name = shift;
+    my $show_private = 0 || shift;
     $template_name =~ s{/}{::}g;
 
     if ($template_name =~ /(.*)::(.*)/) {
         # Qualified name - Need to search for ISA chain
-        my $rv = "$pkg\::$1"->can("_jifty_template_$2");
-        return $rv if $rv;
+        my $class ="$pkg\::$1";
+
+        my $rv = ($class->_resolve_template($2, $show_private));
+        return ($rv) if $rv;
 
         no strict 'refs';
         foreach my $parent (@{"$pkg\::ISA"}) {
             $parent->can('has_template') or next;
-            $rv = $parent->has_template($template_name);
-            return $rv if $rv;
+            ($rv) = $parent->has_template($template_name, $show_private);
+            return ( $rv) if $rv;
         }
-
-        return undef;
     }
     else {
         # Unqualified name
-        return $pkg->can("_jifty_template_$template_name");
+        return ($pkg->_resolve_template($template_name, $show_private));
+    }
+    return undef;
+}
+
+
+sub _resolve_template { 
+    my $self = shift;
+    my $template_name = shift;
+    my $show_private = shift;
+
+   
+
+    my $method =  $self->_template_name_to_sub( $template_name) ;
+    my $private_method; 
+    
+    if ($self->can($method)) {
+        return $method;
     }
+   
+    elsif    ($show_private &&  ($private_method = $self->_template_name_to_private_sub($template_name)) && $self->can($private_method))
+     {  return $private_method;
+ }
+        
+}
+
+sub _template_name_to_sub {
+    my $self = shift;
+    my $template_name =shift;
+        $template_name =~ s/-/_/g;
+    return "_jifty_template_".$template_name;
 
 }
 
+sub _template_name_to_private_sub {
+    my $self = shift;
+    my $template_name =shift;
+        $template_name =~ s/-/_/g;
+    return "_jifty_private_template_".$template_name;
+}
+
+
 1;

Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm	(original)
+++ Template-Declare/lib/Template/Declare/Tags.pm	Sun Dec 10 21:15:34 2006
@@ -2,7 +2,7 @@
 use strict;
 
 package Template::Declare::Tags;
-use vars qw/@EXPORT @EXPORT_OK $self/;
+use vars qw/@EXPORT @EXPORT_OK $PRIVATE $self/;
 use base 'Exporter';
 @EXPORT =
   qw( with template private show get_current_attr attr outs in_isolation $self
@@ -44,19 +44,44 @@
 
     # template "foo" ==> CallerPkg::_jifty_template_foo;
     # template "foo/bar" ==> CallerPkg::foo::_jifty_template_bar;
-    $template_name =~ s{(.*/)?(.*)}{${caller}::$1_jifty_template_$2};
-    $template_name =~ s{/}{::}g;
-
+  
+    my $template_subname;
+    my $base_name;
+    my $template_class;
+    if ($template_name =~ m{(.*/)?(.*)}) {
+        $template_class = caller() . $1;
+        $template_class =~ s{/}{::}g;
+        $base_name = $2;
+    }
+    
+    
     no strict 'refs';
-    *$template_name = sub { shift; goto &$coderef };
+
+    my $codesub = sub { shift; goto &$coderef };
+
+    if ( wantarray) {
+        return ($template_class, $base_name, $codesub);
+    } 
+    else {
+    *{ $template_class .'::' .  $template_class->_template_name_to_sub($base_name); } = $codesub;
+    }
+
+}
+
+sub private (@) {
+    my $class = shift;
+    my $subname = shift;
+    my $code = shift;
+    {
+        no strict 'refs'; 
+        *{ $class .'::' .  $class->_template_name_to_private_sub($subname); } = $code;
+    }
 }
 
 sub get_current_attr ($) {
     $ATTRIBUTES{ $_[0] };
 }
 
-sub private ($) {
-}
 
 our %TagAlternateSpelling = (
     tr => 'row',
@@ -209,13 +234,16 @@
     }
 }
 
-=head2 show [$class or $object] [$template_name or $template_coderef]
+=head2 show [$class or $object] [$template_name or $template_coderef] 
 
 show can either be called with a template name or a package/object and 
 a template.  (It's both functional and OO.)
 
-Displays that template, if it exists. 
+Displays that template, if it exists.
 
+If called from within a Template::Declare subclass, then private
+templates are accessible and visible. If called from something that
+isn't a Template::Declare, only public tempaltes wil be visible.
 
 =cut
 
@@ -228,22 +256,36 @@
     }
 
     my $template = shift;
+
     my $buf      = '';
 
+   
+    my $INSIDE_TEMPLATE = 0;
+    my $caller = caller();
+    if ($caller->isa('Template::Declare')) { $INSIDE_TEMPLATE = 1; }
     {
         local $BUFFER = '';
 
-        my $callable;
+        my ($callable);
         if ( ref($template) eq 'CODE' ) {
             $callable = $template;
         }
         else {
-            $callable = $self->has_template($template);
+            # if we're inside a template, we should show private templates
+           ( $callable)  = $self->has_template($template, $INSIDE_TEMPLATE);
         }
 
         # may want to just use the return value of has_template eventually
-        if ($callable) {
-            &$callable( $self, @_ );
+        
+        if ($callable 
+            
+            and 
+
+            # XXX TODO: die if there's no such template?
+            $self->can($callable)) {
+
+            no strict 'refs';
+            $self->$callable(  @_ );
         }
 
         $buf = $BUFFER;


More information about the Jifty-commit mailing list