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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Wed Nov 29 09:13:25 EST 2006


Author: audreyt
Date: Wed Nov 29 09:13:25 2006
New Revision: 2216

Modified:
   Template-Declare/lib/Template/Declare.pm
   Template-Declare/lib/Template/Declare/Tags.pm

Log:
* Template::Declare - new utility get_current_attr to get the with() context.
* Also allow for qualified template declaration and show.

Modified: Template-Declare/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/lib/Template/Declare.pm	(original)
+++ Template-Declare/lib/Template/Declare.pm	Wed Nov 29 09:13:25 2006
@@ -28,18 +28,27 @@
 
 =cut
 
-
 sub has_template {
     my $pkg = shift;
     my $template_name = shift;
     $template_name =~ s{/}{::}g;
 
     if ($template_name =~ /(.*)::(.*)/) {
-        # Qualified name
-        return "$pkg\::$1"->can("_jifty_template_$2");
+        # Qualified name - Need to search for ISA chain
+        my $rv = "$pkg\::$1"->can("_jifty_template_$2");
+        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;
+        }
+
+        return undef;
     }
     else {
-        # Unqualifid name
+        # Unqualified name
         return $pkg->can("_jifty_template_$template_name");
     }
 

Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm	(original)
+++ Template-Declare/lib/Template/Declare/Tags.pm	Wed Nov 29 09:13:25 2006
@@ -4,22 +4,35 @@
 package Template::Declare::Tags;
 use vars qw/@EXPORT @EXPORT_OK $self/;
 use base 'Exporter';
- at EXPORT_OK = (qw(with));
- at EXPORT = (qw(with template private show outs  outs in_isolation $self));
+ at EXPORT = (qw(with template private show get_current_attr outs in_isolation $self));
 
 our $DEPTH = 0;
 our %ATTRIBUTES = ();
 our $BUFFER = '';
 
 sub outs { $BUFFER .= join('',grep { defined } @_); return ''; }
+
 sub template ($$) {
-    my $templatename = shift;
+    my $template_name = shift;
     my $coderef = shift;
+    my $caller = (caller(0))[0];
+
+    no warnings 'uninitialized';
+    # 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;
+
     no strict 'refs';
-    *{(caller(0))[0]."::_jifty_template_$templatename"} = sub { my $self = shift; $coderef->(@_) } ;
+    *$template_name = sub { shift; goto &$coderef };
+}
 
+sub get_current_attr ($) {
+    $ATTRIBUTES{$_[0]};
+}
+
+sub private ($) {
 }
-sub private ($){}
 
 sub install_tag {
     my $tag = shift;
@@ -31,10 +44,9 @@
 }
 
 
-our %TAGS = ( html => {}, head => {}, title => {}, meta => {}, body => {}, p => {}, hr => {}, br => {}, ul => {}, dl => {}, dt=> {}, dd => {}, ol => {}, li => {}, b => {}, i => {}, em => {}, div => {}, span => {}, form => {}, input => {}, textarea => {}, select => {}, option => {}, a => {}, pre => {}, code => {}, address => {}, iframe => {}, script => {}, h1 => {}, h2=> {}, h3 => {}, h4 => {}, h5 => {}, ); install_tag($_) for(keys %TAGS);
-
-
-
+use CGI ();
+our %TAGS = ( map { $_ => +{} } map { @$_ } @CGI::EXPORT_TAGS{qw/:html2 :html3 :html4 :netscape :form/} );
+install_tag($_) for keys %TAGS;
 
 sub with (@) {
     %ATTRIBUTES = ();
@@ -85,7 +97,7 @@
     return '';
 }
 
-=head2 show [$class or $object] templatename
+=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.)
@@ -97,16 +109,28 @@
 
 sub show {
     $self = shift if ( $_[0]->isa('Template::Declare') );
-    my $templatename = shift;
-    my $buf;
+    my $template = shift;
+    my $buf = '';
+
     {
         local $BUFFER = '';
-        my $callable = "_jifty_template_" . $templatename;
 
-        # may want to just use the return value of has_template eventuall
-        my $ret = $self->$callable(@_) if $self->has_template($templatename);
+        my $callable;
+        if (ref($template) eq 'CODE') {
+            $callable = $template;
+        }
+        else {
+            $callable = $self->has_template($template);
+        }
+
+        # may want to just use the return value of has_template eventually
+        if ($callable) {
+            &$callable($self, @_);
+        }
+
         $buf = $BUFFER;
     }
+
     $BUFFER .= $buf;
     return $buf;
 }


More information about the Jifty-commit mailing list