[Jifty-commit] r2500 - in Template-Declare: lib/Template
lib/Template/Declare t
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Mon Jan 15 20:59:11 EST 2007
Author: jesse
Date: Mon Jan 15 20:59:10 2007
New Revision: 2500
Modified:
Template-Declare/ (props changed)
Template-Declare/lib/Template/Declare.pm
Template-Declare/lib/Template/Declare/Tags.pm
Template-Declare/t/importing.t
Template-Declare/t/self.t
Log:
r20993 at hualien: jesse | 2007-01-15 18:56:56 -0500
Evil, evil, evil tricks to make self the right packagename
Modified: Template-Declare/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/lib/Template/Declare.pm (original)
+++ Template-Declare/lib/Template/Declare.pm Mon Jan 15 20:59:10 2007
@@ -7,11 +7,15 @@
use base 'Class::Data::Inheritable';
__PACKAGE__->mk_classdata('roots');
+__PACKAGE__->mk_classdata('aliases');
+__PACKAGE__->mk_classdata('alias_prefixes');
__PACKAGE__->mk_classdata('templates');
__PACKAGE__->mk_classdata('private_templates');
__PACKAGE__->roots([]);
+__PACKAGE__->aliases([]);
+__PACKAGE__->alias_prefixes({});
__PACKAGE__->templates({});
__PACKAGE__->private_templates({});
@@ -63,6 +67,24 @@
}
+
+=head2 alias
+
+ alias Some::Clever::Mixin under '/mixin';
+
+=cut
+
+sub alias {
+ my $alias_into = caller(0);
+ my $mixin = shift;
+ my $prepend_path = shift;
+
+ push @{$alias_into->aliases()}, $mixin ;
+ $alias_into->alias_prefixes()->{$mixin} = $prepend_path;
+
+}
+
+
=head2 import
@@ -74,31 +96,33 @@
sub import {
return undef if $_[0] eq 'Template::Declare';
- my $import_into = caller(0);
- my $import_from_base = shift;
- my $prepend_path = shift;
+ my $import_into = caller(0);
+ my $import_from_base = shift;
+ my $prepend_path = shift;
my @packages;
{
- no strict 'refs';
- @packages= (@{$import_from_base."::ISA"}, $import_from_base);
+ 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,
- $import_from->_find_template_sub(
- _template_name_to_sub($template_name)
- )
- );
- }
- foreach my $template_name ( @{ __PACKAGE__->private_templates()->{$import_from} } ) {
- my $code = $import_from->_find_template_sub( _template_name_to_private_sub($template_name));
- $import_into->register_private_template(
- $prepend_path."/".$template_name, $code
- );
+ foreach my $import_from (@packages) {
+ foreach my $template_name (
+ @{ __PACKAGE__->templates()->{$import_from} } ) {
+ $import_into->register_template(
+ $prepend_path . "/" . $template_name,
+ $import_from->_find_template_sub(
+ _template_name_to_sub($template_name)
+ )
+ );
+ }
+ foreach my $template_name (
+ @{ __PACKAGE__->private_templates()->{$import_from} } ) {
+ my $code = $import_from->_find_template_sub(
+ _template_name_to_private_sub($template_name) );
+ $import_into->register_private_template(
+ $prepend_path . "/" . $template_name, $code );
+ }
}
-}
}
@@ -144,9 +168,21 @@
my $show_private = shift || 0;
foreach my $package ( reverse @{ Template::Declare->roots } ) {
- if ( my $coderef = $package->has_template($template_name, $show_private) ) {
+ if ( my $coderef = $package->has_template( $template_name, $show_private ) ) {
return $coderef;
- }
+ }
+
+ foreach my $alias_class ( @{ $package->aliases } ) {
+ my $alias_prefix = $package->alias_prefixes()->{$alias_class};
+ $template_name = "/$template_name";
+ if ( $template_name =~ m{$alias_prefix/(.*)$} ) {
+ my $dispatch_to_template = $1;
+ if (my $coderef = $alias_class->has_template( $dispatch_to_template, $show_private)) {
+ # We're going to force $self to the aliased class
+ return sub { &$coderef($alias_class) };
+ }
+ }
+ }
}
return undef;
}
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 20:59:10 2007
@@ -162,9 +162,7 @@
my $field = shift;
my $val = shift;
- $val = _escape_utf8($val);
- $buf .= qq[ $field="$val"];
-
+ $buf .= ' '. $field .q{="} . _escape_utf8($val) .q{"};
wantarray ? () : '';
};
@@ -191,11 +189,8 @@
$BUFFER .= $buf;
$BUFFER .= "\n" . ( " " x $DEPTH ) if ( $buf =~ /\n/ );
$BUFFER .= "</$tag>";
- } elsif ( $tag
- =~ m{\A(?: base | meta | link | hr | br | param | img | area | input | col )\z}x
- )
- {
-
+ } elsif ( $tag =~ m{\A(?: base | meta | link | hr | br | param | img | area | input | col )\z}x) {
+ # XXX TODO: This should come out of HTML::Tagset
# EMPTY tags can close themselves.
$BUFFER .= $buf . " />";
} else {
@@ -204,11 +199,7 @@
$BUFFER .= $buf . "></$tag>";
}
- if ($more_code) {
- $more_code->();
- } else {
- return '';
- }
+ return $more_code ? $more_code->() : '';
}
=head2 show [$class or $object] [$template_name or $template_coderef]
@@ -232,18 +223,18 @@
sub show {
my $template = shift;
- my $buf = '';
my $INSIDE_TEMPLATE = 0;
- my $caller = caller();
# if we're inside a template, we should show private templates
- if ( $caller->isa('Template::Declare') ) { $INSIDE_TEMPLATE = 1; }
+ if ( caller()->isa('Template::Declare') ) { $INSIDE_TEMPLATE = 1; }
my $callable =
ref($template) eq 'CODE'
? $template
: Template::Declare->has_template( $template, $INSIDE_TEMPLATE );
+
return '' unless ($callable);
+ my $buf = '';
{
local $BUFFER = '';
&$callable($self);
Modified: Template-Declare/t/importing.t
==============================================================================
--- Template-Declare/t/importing.t (original)
+++ Template-Declare/t/importing.t Mon Jan 15 20:59:10 2007
@@ -7,7 +7,7 @@
template 'imported' => sub {
my $self = shift;
- div { outs( 'This is imported from' . $self ) };
+ div { outs( 'This is imported from ' . $self ) };
};
package Wifty::UI::imported_subclass_pkg;
@@ -55,7 +55,7 @@
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',
+ like( $simple, qr'Wifty::UI',
'$self is correct in template block' );
ok_lint($simple);
}
@@ -71,10 +71,9 @@
);
like(
$simple,
- qr'Wifty::UI::imported_subclass_pkg',
+ qr'Wifty::UI',
'$self is correct in template block'
);
ok_lint($simple);
}
-
1;
Modified: Template-Declare/t/self.t
==============================================================================
--- Template-Declare/t/self.t (original)
+++ Template-Declare/t/self.t Mon Jan 15 20:59:10 2007
@@ -7,7 +7,6 @@
template simple => sub {
my $self = shift;
- warn "in simple my self is $self";
html {
head {};
body { outs( 'This is my content from' . $self ); };
More information about the Jifty-commit
mailing list