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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Jan 15 17:04:25 EST 2007


Author: jesse
Date: Mon Jan 15 17:04:23 2007
New Revision: 2497

Added:
   Template-Declare/t/self.t
Modified:
   Template-Declare/   (props changed)
   Template-Declare/lib/Template/Declare/Tags.pm
   Template-Declare/t/importing.t
   Template-Declare/t/subclassing.t
   Template-Declare/t/trivial.t

Log:
 r20989 at hualien:  jesse | 2007-01-15 17:03:19 -0500
 * work on making sure there's a $self available to the template


Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm	(original)
+++ Template-Declare/lib/Template/Declare/Tags.pm	Mon Jan 15 17:04:23 2007
@@ -8,8 +8,7 @@
 use Carp;
 
 @EXPORT =
-    qw( with template private show get_current_attr attr outs outs_raw in_isolation $self under
-    Tr td );    # these two warns the user to use row/cell instead
+    qw( with template private show get_current_attr attr outs outs_raw in_isolation $self under Tr td );    # these two warns the user to use row/cell instead
 
 sub under ($) { return shift }
 
@@ -57,16 +56,14 @@
 
     # template "foo" ==> CallerPkg::_jifty_template_foo;
     # template "foo/bar" ==> CallerPkg::_jifty_template_foo/bar;
+    my $codesub = sub { local $self = shift if ($_[0]); &$coderef($self) };
 
-    no strict 'refs';
-
-    my $codesub = sub { shift; goto &$coderef };
-
-    if (wantarray) {
+    if (wantarray) { 
+        # We're being called by something like private that doesn't want us to register ourselves
         return ( $template_class, $template_name, $codesub );
     } else {
-        Template::Declare::register_template( $template_class, $template_name,
-            $codesub );
+        # We've been called in a void context and should register this template
+        Template::Declare::register_template( $template_class, $template_name, $codesub );
     }
 
 }
@@ -244,7 +241,6 @@
     my $template = shift;
 
     my $buf = '';
-
     my $INSIDE_TEMPLATE = 0;
     my $caller          = caller();
     if ( $caller->isa('Template::Declare') ) { $INSIDE_TEMPLATE = 1; }
@@ -255,16 +251,14 @@
         if ( ref($template) eq 'CODE' ) {
             $callable = $template;
         } else {
-
             # if we're inside a template, we should show private templates
-            $callable = Template::Declare->has_template( $template,
-                $INSIDE_TEMPLATE );
+            $callable = Template::Declare->has_template( $template, $INSIDE_TEMPLATE );
         }
 
         # may want to just use the return value of has_template eventually
         if ($callable) {
-            no strict 'refs';
-            &$callable( $self, @_ );
+            # no strict 'refs';
+            &$callable( $self );
         }
         $buf = $BUFFER;
     }

Modified: Template-Declare/t/importing.t
==============================================================================
--- Template-Declare/t/importing.t	(original)
+++ Template-Declare/t/importing.t	Mon Jan 15 17:04:23 2007
@@ -6,6 +6,7 @@
 use Template::Declare::Tags;
 
 template 'imported' => sub {
+    my $self = shift;
     div { outs( 'This is imported from' . $self ) };
 };
 
@@ -27,6 +28,7 @@
 };
 
 private template 'private-content' => sub {
+    my $self = shift;
     with( id => 'body' ), div {
         outs( 'This is my content from' . $self );
     };
@@ -50,6 +52,7 @@
 
 {
     local $Template::Declare::Tags::BUFFER;
+    local $Template::Declare::Tags::self = 'Wifty::UI';
     my $simple = ( show('imported_pkg/imported') );
     like( $simple, qr'This is imported' );
     like( $simple, qr'Wifty::UI::imported_pkg',
@@ -59,6 +62,7 @@
 
 {
     local $Template::Declare::Tags::BUFFER;
+    local $Template::Declare::Tags::self = 'Wifty::UI';
     my $simple = ( show('imported_subclass_pkg/imported') );
     like(
         $simple,

Added: Template-Declare/t/self.t
==============================================================================
--- (empty file)
+++ Template-Declare/t/self.t	Mon Jan 15 17:04:23 2007
@@ -0,0 +1,39 @@
+use warnings;
+use strict;
+
+package Wifty::UI;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+
+template simple => sub {
+    my $self = shift;
+    warn "in simple my self is $self";
+    html {
+        head {};
+        body { outs( 'This is my content from' . $self ); };
+        }
+
+};
+
+private template 'private-content' => sub {
+    with( id => 'body' ), div {
+        outs('This is my content from'.$self);
+    };
+};
+
+package main;
+Template::Declare->init(roots => ['Wifty::UI']);
+
+use Test::More tests => 3;
+require "t/utils.pl";
+{
+    local $Template::Declare::Tags::BUFFER;
+    local $Template::Declare::Tags::self = 'Wifty::UI';
+    my $simple =  Template::Declare::Tags::show('simple') ;
+    like( $simple,  qr'This is my content' );
+    like( $simple,  qr'Wifty::UI', '$self is correct in template block' );
+    ok_lint($simple);
+}
+
+
+1;

Modified: Template-Declare/t/subclassing.t
==============================================================================
--- Template-Declare/t/subclassing.t	(original)
+++ Template-Declare/t/subclassing.t	Mon Jan 15 17:04:23 2007
@@ -15,6 +15,7 @@
 };
 
 private template 'private-content' => sub {
+    my $self = shift;
     with( id => 'body' ), div {
         outs('This is my content from'.$self);
     };
@@ -55,8 +56,8 @@
 
 {
     local $Template::Declare::Tags::BUFFER;
-    local $self = 'Wifty::UI';
-    my $simple =  Wifty::UI->show('simple') ;
+    local $Template::Declare::Tags::self = 'Wifty::UI';
+    my $simple =  Template::Declare::Tags::show('simple') ;
     like( $simple,  qr'This is my content' );
     like( $simple,  qr'Wifty::UI', '$self is correct in template block' );
     ok_lint($simple);

Modified: Template-Declare/t/trivial.t
==============================================================================
--- Template-Declare/t/trivial.t	(original)
+++ Template-Declare/t/trivial.t	Mon Jan 15 17:04:23 2007
@@ -26,7 +26,7 @@
 };
 
 
-template wrapper => sub {
+sub  wrapper {
     my ( $title, $coderef) = (@_);
     outs '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
         with ( xmlns      => "http://www.w3.org/1999/xhtml", 'xml:lang' => "en"), 
@@ -45,9 +45,7 @@
 
 template markup => sub {
     my $self = shift;
-
-    show(
-        'wrapper',
+    wrapper(
         'My page!',
         sub {
 


More information about the Jifty-commit mailing list