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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Jan 15 15:56:45 EST 2007


Author: jesse
Date: Mon Jan 15 15:56:41 2007
New Revision: 2495

Added:
   Template-Declare/t/importing.t
   Template-Declare/t/subclassing.t
Removed:
   Template-Declare/t/roots.t
Modified:
   Template-Declare/   (props changed)
   Template-Declare/lib/Template/Declare.pm

Log:
 r20982 at hualien:  jesse | 2007-01-15 15:54:40 -0500
 * allow imported subclass items


Modified: Template-Declare/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/lib/Template/Declare.pm	(original)
+++ Template-Declare/lib/Template/Declare.pm	Mon Jan 15 15:56:41 2007
@@ -75,9 +75,15 @@
 sub import {
     return undef if $_[0] eq 'Template::Declare';
     my $import_into  = caller(0);
-    my $import_from  = shift;
+    my $import_from_base  = shift;
     my $prepend_path = shift;
 
+    my @packages;
+    {
+    no strict 'refs';
+    @packages= (@{$import_from_base."::ISA"}, $import_from_base);
+    }
+    foreach my $import_from  (@packages) { 
     foreach my $template_name ( @{ __PACKAGE__->templates()->{$import_from} } ) {
         $import_into->register_template(
             $prepend_path."/".$template_name,
@@ -88,11 +94,11 @@
     }
     foreach my $template_name ( @{ __PACKAGE__->private_templates()->{$import_from} } ) {
             my $code = $import_from->_find_template_sub( _template_name_to_private_sub($template_name));
-            warn "HEY NO CODE FOR private template  $template_name in $import_from" unless ($code);
         $import_into->register_private_template(
             $prepend_path."/".$template_name, $code
         );
     }
+}
 
 }
 

Added: Template-Declare/t/importing.t
==============================================================================
--- (empty file)
+++ Template-Declare/t/importing.t	Mon Jan 15 15:56:41 2007
@@ -0,0 +1,89 @@
+use warnings;
+use strict;
+
+package Wifty::UI::imported_pkg;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+
+template 'imported' => sub {
+    div { outs( 'This is imported from' . $self ) };
+};
+
+package Wifty::UI::imported_subclass_pkg;
+use base qw/Wifty::UI::imported_pkg/;
+use Template::Declare::Tags;
+
+package Wifty::UI;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+
+template simple => sub {
+
+    html {
+        head {};
+        body { show 'private-content'; };
+        }
+
+};
+
+private template 'private-content' => sub {
+    with( id => 'body' ), div {
+        outs( 'This is my content from' . $self );
+    };
+};
+
+import Wifty::UI::imported_pkg under '/imported_pkg';
+import Wifty::UI::imported_subclass_pkg under '/imported_subclass_pkg';
+
+package main;
+use Template::Declare::Tags;
+Template::Declare->init( roots => ['Wifty::UI'] );
+
+use Test::More tests => 10;
+use HTML::Lint;
+
+ok( Wifty::UI::imported_pkg->has_template('imported') );
+ok( Wifty::UI::imported_subclass_pkg->has_template('imported') );
+
+ok( Template::Declare->has_template('imported_pkg/imported') );
+ok( Template::Declare->has_template('imported_subclass_pkg/imported'), "When you subclass and then import, the superclass's imports are there" );
+
+{
+    local $Template::Declare::Tags::BUFFER;
+    my $simple = ( show('imported_pkg/imported') );
+    like( $simple, qr'This is imported' );
+    like( $simple, qr'Wifty::UI::imported_pkg',
+        '$self is correct in template block' );
+    ok_lint($simple);
+}
+
+{
+    local $Template::Declare::Tags::BUFFER;
+    my $simple = ( show('imported_subclass_pkg/imported') );
+    like(
+        $simple,
+        qr'This is imported',
+        "We got the imported version in the subclass"
+    );
+    like(
+        $simple,
+        qr'Wifty::UI::imported_subclass_pkg',
+        '$self is correct in template block'
+    );
+    ok_lint($simple);
+}
+
+sub ok_lint {
+    my $html = shift;
+
+    my $lint = HTML::Lint->new;
+
+    $lint->parse($html);
+    is( $lint->errors, 0, "Lint checked clean" );
+    foreach my $error ( $lint->errors ) {
+        diag( $error->as_string );
+    }
+
+}
+
+1;

Added: Template-Declare/t/subclassing.t
==============================================================================
--- (empty file)
+++ Template-Declare/t/subclassing.t	Mon Jan 15 15:56:41 2007
@@ -0,0 +1,103 @@
+use warnings;
+use strict;
+
+package Wifty::UI;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+
+template simple => sub {
+
+    html {
+        head {};
+        body { show 'private-content'; };
+        }
+
+};
+
+private template 'private-content' => sub {
+    with( id => 'body' ), div {
+        outs('This is my content from'.$self);
+    };
+};
+
+
+
+package Baseclass::UI;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+private template 'private-content' => sub {
+    with( id => 'body' ), div {
+        outs('This is baseclass content');
+    };
+
+};
+
+
+package Childclass::UI;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+private template 'private-content' => sub {
+    with( id => 'body' ), div {
+        outs('This is child class content');
+    };
+
+};
+
+
+
+
+package main;
+use Template::Declare::Tags;
+Template::Declare->init(roots => ['Baseclass::UI', 'Wifty::UI']);
+
+use Test::More tests => 9;
+use HTML::Lint;
+
+
+{
+    local $Template::Declare::Tags::BUFFER;
+    local $self = 'Wifty::UI';
+    my $simple =  Wifty::UI->show('simple') ;
+    like( $simple,  qr'This is my content' );
+    like( $simple,  qr'Wifty::UI', '$self is correct in template block' );
+    ok_lint($simple);
+}
+
+
+Template::Declare->init(
+    roots => [ 'Baseclass::UI', 'Wifty::UI', 'Childclass::UI' ] );
+{
+    local $Template::Declare::Tags::BUFFER;
+    my $simple = ( show('simple') );
+    like( $simple, qr'This is child class content' );
+    ok_lint($simple);
+}
+
+{
+    local $Template::Declare::Tags::BUFFER;
+    my $simple = ( show('does_not_exist') );
+    unlike( $simple , qr'This is my content' );
+    ok_lint($simple);
+}
+{
+    local $Template::Declare::Tags::BUFFER;
+    my $simple = ( show('private-content') );
+    unlike( $simple , qr'This is my content', "Can't call private templates" );
+    ok_lint($simple);
+}
+
+
+sub ok_lint {
+    my $html = shift;
+
+    my $lint = HTML::Lint->new;
+
+    $lint->parse($html);
+    is( $lint->errors, 0, "Lint checked clean" );
+    foreach my $error ( $lint->errors ) {
+        diag( $error->as_string );
+    }
+
+}
+
+1;


More information about the Jifty-commit mailing list