[Jifty-commit] r2244 - Template-Declare/lib/Template/Declare

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Dec 1 02:32:45 EST 2006


Author: audreyt
Date: Fri Dec  1 02:32:44 2006
New Revision: 2244

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

Log:
* Template::Declare - Support for declarative "is" copula on attributes.
  For example:

    table {{width is '100%'} row {
        cell {{width is '50%'} $search_region->render };
        cell {{width is '50%'} $result_region->render };
    } };


Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm	(original)
+++ Template-Declare/lib/Template/Declare/Tags.pm	Fri Dec  1 02:32:44 2006
@@ -9,6 +9,7 @@
 
 our $DEPTH = 0;
 our %ATTRIBUTES = ();
+our %CURRENT_ATTRIBUTES = ();
 our $BUFFER = '';
 
 sub Tr (&) { die "tr {...} and td {...} are invalid; use row {...} and cell {...} instead." }
@@ -32,29 +33,30 @@
 }
 
 sub get_current_attr ($) {
-    $ATTRIBUTES{$_[0]};
+    $CURRENT_ATTRIBUTES{$_[0]};
 }
 
 sub private ($) {
 }
 
-use constant TagAlternateSpelling => {
+our %TagAlternateSpelling = (
     Tr   => 'row',
     td   => 'cell',
     base => '', # Currently 'base' has no alternate spellings; simply ignore it
-};
+);
 
 sub install_tag {
     my $tag = shift;
     my $name = $tag;
 
-    if (exists(TagAlternateSpelling->{$tag})) {
-        $name = TagAlternateSpelling->{$tag} or return;
+    if (exists($TagAlternateSpelling{$tag})) {
+        $name = $TagAlternateSpelling{$tag} or return;
     }
 
     push @EXPORT, $name;
 
     no strict 'refs';
+    no warnings 'redefine';
     *$name = sub (&) { local *__ANON__ = $tag; _tag(@_) };
 }
 
@@ -87,27 +89,39 @@
 
     my $tag = $subroutine;
     $tag =~ s/^.*\:\://;
-    $BUFFER .= "\n" . ( " " x $DEPTH ) . "<$tag"
-        . join( '',
-        map { qq{ $_="} . ( $ATTRIBUTES{$_} || '' ) . qq{"} }
-            keys %ATTRIBUTES );
-    %ATTRIBUTES = ();
 
     my $buf;
     {
-        local $BUFFER = '';
-        local $DEPTH  = $DEPTH + 1;
-        my $last = $code->() || '';
+        no warnings qw( uninitialized redefine once );
 
-        $buf = $BUFFER;
+        local $BUFFER;
+        local $DEPTH = $DEPTH + 1;
+        local %CURRENT_ATTRIBUTES = %ATTRIBUTES;
+        %ATTRIBUTES = ();
+
+        local *is::AUTOLOAD = sub {
+            my $field = our $AUTOLOAD;
+            $field =~ s/.*:://;
+            $CURRENT_ATTRIBUTES{$field} = join(' ', @_[1..$#_]);
+            '';
+        };
+
+        my $last = $code->();
+
+        $buf = "\n" . ( " " x ($DEPTH-1) ) . "<$tag"
+            . join( '',
+            map { qq{ $_="} . ( $CURRENT_ATTRIBUTES{$_} || '' ) . qq{"} }
+                keys %CURRENT_ATTRIBUTES) . '>';
+
+        $buf .= $BUFFER;
 
 # We concatenate "" to force scalarization when $last or $BUFFER is solely a Jifty::Web::Link
-        $buf .= $last unless ($BUFFER);    # HACK WRONG;
+        $buf .= $last unless length($BUFFER);    # HACK WRONG;
     }
 
     # default to <tag/> rather than <tag></tag> if there's no content
     if ($buf) {
-        $BUFFER .= ( ">" . $buf );
+        $BUFFER .= $buf;
         $BUFFER .= "\n" . ( " " x $DEPTH ) if ( $buf =~ /\n/ );
         $BUFFER .= "</$tag>";
     } else {


More information about the Jifty-commit mailing list