[Jifty-commit] r3260 - in Template-Declare: lib/Template
lib/Template/Declare
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Sat May 19 15:43:23 EDT 2007
Author: jesse
Date: Sat May 19 15:43:22 2007
New Revision: 3260
Modified:
Template-Declare/ (props changed)
Template-Declare/lib/Template/Declare.pm
Template-Declare/lib/Template/Declare/Buffer.pm
Template-Declare/lib/Template/Declare/Tags.pm
Log:
r56973 at pinglin: jesse | 2007-05-19 15:13:55 -0400
perltidy
Modified: Template-Declare/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/lib/Template/Declare.pm (original)
+++ Template-Declare/lib/Template/Declare.pm Sat May 19 15:43:22 2007
@@ -17,19 +17,17 @@
__PACKAGE__->mk_classdata('buffer_stack');
__PACKAGE__->mk_classdata('imported_into');
-
-__PACKAGE__->roots([]);
-__PACKAGE__->aliases({});
-__PACKAGE__->alias_metadata({});
-__PACKAGE__->templates({});
-__PACKAGE__->private_templates({});
-__PACKAGE__->buffer_stack([]);
+__PACKAGE__->roots( [] );
+__PACKAGE__->aliases( {} );
+__PACKAGE__->alias_metadata( {} );
+__PACKAGE__->templates( {} );
+__PACKAGE__->private_templates( {} );
+__PACKAGE__->buffer_stack( [] );
__PACKAGE__->new_buffer_frame();
use vars qw/$TEMPLATE_VARS/;
-
=head1 NAME
Template::Declare - Perlish declarative templates
@@ -161,31 +159,31 @@
sub init {
my $class = shift;
- my %args = (@_);
+ my %args = (@_);
- if ($args{'roots'}) {
- $class->roots($args{'roots'});
+ if ( $args{'roots'} ) {
+ $class->roots( $args{'roots'} );
}
}
-
sub new_buffer_frame {
my $buffer = Template::Declare::Buffer->new();
- unshift @{__PACKAGE__->buffer_stack}, $buffer;
+ unshift @{ __PACKAGE__->buffer_stack }, $buffer;
}
sub end_buffer_frame {
- shift @{__PACKAGE__->buffer_stack};
+ shift @{ __PACKAGE__->buffer_stack };
}
sub buffer {
- unless (__PACKAGE__->buffer_stack->[0] ) { Carp::confess(__PACKAGE__."->buffer called with no buffer");}
- return __PACKAGE__->buffer_stack->[0] ;
+ unless ( __PACKAGE__->buffer_stack->[0] ) {
+ Carp::confess( __PACKAGE__ . "->buffer called with no buffer" );
+ }
+ return __PACKAGE__->buffer_stack->[0];
}
-
=head2 show TEMPLATE_NAME
Call C<show> with a C<template_name> and C<Template::Declare> will
@@ -201,13 +199,12 @@
=cut
sub show {
- my $class = shift;
+ my $class = shift;
my $template = shift;
- local %Template::Declare::Tags::ELEMENT_ID_CACHE = () ;
+ local %Template::Declare::Tags::ELEMENT_ID_CACHE = ();
return Template::Declare::Tags::show_page($template);
}
-
=head2 alias
alias Some::Clever::Mixin under '/mixin';
@@ -233,7 +230,6 @@
}
-
=head2 import_templates
@@ -242,7 +238,6 @@
=cut
-
sub import_templates {
return undef if $_[0] eq 'Template::Declare';
my $import_into = caller(0);
@@ -260,7 +255,8 @@
}
foreach my $import_from (@packages) {
foreach my $template_name (
- @{ __PACKAGE__->templates()->{$import_from} } ) {
+ @{ __PACKAGE__->templates()->{$import_from} } )
+ {
$import_into->register_template(
$prepend_path . "/" . $template_name,
$import_from->_find_template_sub(
@@ -269,7 +265,8 @@
);
}
foreach my $template_name (
- @{ __PACKAGE__->private_templates()->{$import_from} } ) {
+ @{ __PACKAGE__->private_templates()->{$import_from} } )
+ {
my $code = $import_from->_find_template_sub(
_template_name_to_private_sub($template_name) );
$import_into->register_private_template(
@@ -287,13 +284,12 @@
=cut
sub path_for {
- my ($class, $template) = @_;
+ my ( $class, $template ) = @_;
my $prepend = $class->imported_into;
$prepend = '' unless defined $prepend;
return $prepend . '/' . $template;
}
-
=head2 has_template PACKAGE TEMPLATE_NAME SHOW_PRIVATE
Takes a package, template name and a boolean. The boolean determines whether to show private templates.
@@ -307,11 +303,12 @@
# When using Template::Declare->has_template, find in all
# registered namespaces.
-# if ( $_[0] eq 'Template::Declare' ) {
- &resolve_template(@_) || _has_template(@_);
-# } else {
-# _has_template(@_);
-# }
+ # if ( $_[0] eq 'Template::Declare' ) {
+ &resolve_template(@_) || _has_template(@_);
+
+ # } else {
+ # _has_template(@_);
+ # }
}
sub _has_template {
@@ -321,10 +318,17 @@
my $template_name = shift;
my $show_private = 0 || shift;
- if ( my $coderef = $pkg->_find_template_sub( _template_name_to_sub($template_name) ) ) {
+ if ( my $coderef
+ = $pkg->_find_template_sub( _template_name_to_sub($template_name) ) )
+ {
return $coderef;
- }
- elsif ( $show_private and $coderef = $pkg->_find_template_sub( _template_name_to_private_sub($template_name)) ) {
+ } elsif (
+ $show_private
+ and $coderef = $pkg->_find_template_sub(
+ _template_name_to_private_sub($template_name)
+ )
+ )
+ {
return $coderef;
}
@@ -345,40 +349,47 @@
=cut
-
sub resolve_template {
my $self = shift;
my $template_name = shift;
my $show_private = shift || 0;
my @search_roots;
- if ($self eq 'Template::Declare') {
+ if ( $self eq 'Template::Declare' ) {
@search_roots = reverse @{ Template::Declare->roots };
} else {
@search_roots = ($self);
}
- foreach my $package ( @search_roots) {
- unless ($package and $package->isa('Template::Declare')) {
+ foreach my $package (@search_roots) {
+ unless ( $package and $package->isa('Template::Declare') ) {
next;
}
- 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_key ( @{ Template::Declare->aliases->{$package} } ) {
- my $alias_info = $package->alias_metadata()->{$alias_key};
+ foreach my $alias_key ( @{ Template::Declare->aliases->{$package} } )
+ {
+ my $alias_info = $package->alias_metadata()->{$alias_key};
my $alias_prefix = $alias_info->{path};
- my $alias_class = $alias_info->{class};
+ my $alias_class = $alias_info->{class};
my $package_vars = $alias_info->{package_vars};
- $template_name = "/$template_name" ;
+ $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)) {
+ if (my $coderef = $alias_class->_has_template(
+ $dispatch_to_template, $show_private
+ )
+ )
+ {
+
# We're going to force $self to the aliased class
return sub {
no strict 'refs';
@@ -386,8 +397,13 @@
local $TEMPLATE_VARS->{$alias_class} = $package_vars;
&$coderef($alias_class);
};
- }
- elsif ( $coderef = $alias_class->resolve_template( $dispatch_to_template, $show_private)) {
+ } elsif (
+ $coderef = $alias_class->resolve_template(
+ $dispatch_to_template, $show_private
+ )
+ )
+ {
+
# We're going to force $self to the aliased class
return sub {
no strict 'refs';
@@ -397,38 +413,33 @@
};
}
-
-
-
-
}
}
}
}
-
sub _find_template_sub {
- my $self = shift;
+ my $self = shift;
my $subname = shift;
return $self->can($subname);
}
sub _template_name_to_sub {
- return _subname( "_jifty_template_", shift);
+ return _subname( "_jifty_template_", shift );
}
sub _template_name_to_private_sub {
- return _subname( "_jifty_private_template_", shift);
+ return _subname( "_jifty_private_template_", shift );
}
sub _subname {
my $prefix = shift;
- my $template = shift ||'';
+ my $template = shift || '';
$template =~ s{^/+}{};
$template =~ s{/+}{/}g;
- return join ('', $prefix,$template);
+ return join( '', $prefix, $template );
}
=head2 register_template PACKAGE TEMPLATE_NAME CODEREF
@@ -479,12 +490,22 @@
*{ $class . '::' . $subname } = $coderef;
}
-
sub package_variable {
my $self = shift;
- my $var = shift;
+ my $var = shift;
+ if (@_) {
+ $TEMPLATE_VARS->{$self}->{$var} = shift;
+ }
return $TEMPLATE_VARS->{$self}->{$var};
+}
+sub package_variables {
+ my $self = shift;
+ my $var = shift;
+ if (@_) {
+ %{ $TEMPLATE_VARS->{$self} } = shift;
+ }
+ return $TEMPLATE_VARS->{$self};
}
=head1 BUGS
Modified: Template-Declare/lib/Template/Declare/Buffer.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Buffer.pm (original)
+++ Template-Declare/lib/Template/Declare/Buffer.pm Sat May 19 15:43:22 2007
@@ -7,11 +7,11 @@
__PACKAGE__->mk_accessors('data');
sub append {
- my $self = shift;
+ my $self = shift;
my $content = shift;
no warnings 'uninitialized';
- $self->data($self->data.$content);
+ $self->data( $self->data . $content );
}
sub clear {
@@ -19,5 +19,4 @@
$self->data('');
}
-
1;
Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm (original)
+++ Template-Declare/lib/Template/Declare/Tags.pm Sat May 19 15:43:22 2007
@@ -8,12 +8,13 @@
use base 'Exporter';
use Carp;
- at EXPORT = qw( with template private show attr outs outs_raw in_isolation $self under get_current_attr smart_tag_wrapper );
-push @EXPORT, qw(Tr td ); # these two warns the user to use row/cell instead
+ at EXPORT
+ = qw( with template private show attr outs outs_raw in_isolation $self under get_current_attr smart_tag_wrapper );
+push @EXPORT, qw(Tr td ); # these two warns the user to use row/cell instead
-our %ATTRIBUTES = ();
+our %ATTRIBUTES = ();
our %ELEMENT_ID_CACHE = ();
-our $DEPTH = 0;
+our $DEPTH = 0;
=head1 NAME
@@ -43,16 +44,22 @@
# template "foo" ==> CallerPkg::_jifty_template_foo;
# template "foo/bar" ==> CallerPkg::_jifty_template_foo/bar;
- my $codesub = sub { local $self = $_[0] || $self || $template_class;
- #local $self = $template_class unless $self;
- &$coderef($self) };
+ my $codesub = sub {
+ local $self = $_[0] || $self || $template_class;
- if (wantarray) {
- # We're being called by something like private that doesn't want us to register ourselves
+ #local $self = $template_class unless $self;
+ &$coderef($self);
+ };
+
+ 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 {
- # We've been called in a void context and should register this template
- 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 );
}
}
@@ -63,7 +70,6 @@
=cut
-
sub private (@) {
my $class = shift;
my $subname = shift;
@@ -88,10 +94,9 @@
=cut
-
sub attr (&;@) {
my $code = shift;
- my @rv = $code->();
+ my @rv = $code->();
while ( my ( $field, $val ) = splice( @rv, 0, 2 ) ) {
# only defined whle in a tag context
@@ -100,7 +105,6 @@
return @_;
}
-
=head2 outs STUFF
C<outs> HTML-encodes its arguments and appends them to C<Template::Declare>'s output buffer.
@@ -118,10 +122,8 @@
#sub outs_raw { Template::Declare->buffer->append( join( '', grep {defined} @_ )); return ''; }
-
-
-sub outs { _outs(0, @_); }
-sub outs_raw { _outs(1, @_); }
+sub outs { _outs( 0, @_ ); }
+sub outs_raw { _outs( 1, @_ ); }
sub _outs {
my $raw = shift;
@@ -132,15 +134,16 @@
foreach my $item ( grep {defined} @phrases ) {
Template::Declare->new_buffer_frame;
- my $returned = ref($item) eq 'CODE'
+ my $returned =
+ ref($item) eq 'CODE'
? $item->()
- : ( $raw ? $item : _escape_utf8($item) ) ||'';
- my $content = Template::Declare->buffer->data ||'';
+ : ( $raw ? $item : _escape_utf8($item) ) || '';
+ my $content = Template::Declare->buffer->data || '';
Template::Declare->end_buffer_frame;
Template::Declare->buffer->append( $content . $returned );
}
- $buf = Template::Declare->buffer->data ||'';
+ $buf = Template::Declare->buffer->data || '';
Template::Declare->end_buffer_frame;
if ( defined wantarray and not wantarray ) {
return $buf;
@@ -168,7 +171,6 @@
'', # Currently 'base' has no alternate spellings; simply ignore it
);
-
=head2 install_tag TAGNAME
Sets up TAGNAME as a tag that can be used in user templates.
@@ -179,7 +181,6 @@
=cut
-
sub install_tag {
my $tag = lc( $_[0] );
my $name = $tag;
@@ -199,7 +200,7 @@
# Scalar context - return a coderef that represents ourselves.
my @__ = @_;
my $_self = $self;
- my $sub = sub {
+ my $sub = sub {
local $self = $_self;
local *__ANON__ = $tag;
_tag(@__);
@@ -219,7 +220,6 @@
);
install_tag($_) for keys %TAGS;
-
=head2 with
C<with> is an alternative way to specify attributes for a tag:
@@ -236,7 +236,6 @@
=cut
-
sub with (@) {
%ATTRIBUTES = ();
while ( my ( $key, $val ) = splice( @_, 0, 2 ) ) {
@@ -244,9 +243,10 @@
$ATTRIBUTES{$key} = $val;
if ( lc($key) eq 'id' ) {
- if ( $ELEMENT_ID_CACHE{$val}++) {
- warn "HTML appears to contain illegal duplicate element id: $val";
- }
+ if ( $ELEMENT_ID_CACHE{$val}++ ) {
+ warn
+ "HTML appears to contain illegal duplicate element id: $val";
+ }
}
}
@@ -289,18 +289,17 @@
Template::Declare->new_buffer_frame;
my $last = join '', #
- map { ref($_) ? $_ : _escape_utf8($_) } #
- $coderef->(%ATTRIBUTES);
+ map { ref($_) ? $_ : _escape_utf8($_) } #
+ $coderef->(%ATTRIBUTES);
- %ATTRIBUTES = (); # prevent leakage
+ %ATTRIBUTES = (); # prevent leakage
if ( length( Template::Declare->buffer->data ) ) {
# We concatenate to force scalarization when $last or
# $Template::Declare->buffer is solely a Jifty::Web::Link
$buf .= Template::Declare->buffer->data;
- }
- elsif ( length $last ) {
+ } elsif ( length $last ) {
$buf .= $last;
}
@@ -352,7 +351,7 @@
my $field = shift;
my $val = shift;
- $buf .= ' '. $field .q{="} . _escape_utf8($val) .q{"};
+ $buf .= ' ' . $field . q{="} . _escape_utf8($val) . q{"};
wantarray ? () : '';
};
@@ -361,7 +360,7 @@
Template::Declare->new_buffer_frame;
my $last = join '', map { ref($_) ? $_ : _escape_utf8($_) } $code->();
- if ( length(Template::Declare->buffer->data) ) {
+ if ( length( Template::Declare->buffer->data ) ) {
# We concatenate to force scalarization when $last or $Template::Declare->buffer is solely a Jifty::Web::Link
$buf .= '>' . Template::Declare->buffer->data;
@@ -372,7 +371,7 @@
} else {
$had_content = 0;
}
-
+
Template::Declare->end_buffer_frame;
}
@@ -380,19 +379,24 @@
if ($had_content) {
$buf .= "\n" . ( " " x $DEPTH ) if ( $buf =~ /\n/ );
$buf .= "</$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.
- $buf .= " />";
+ $buf .= " />";
} else {
# Otherwise we supply a closing tag.
$buf .= "></$tag>";
}
-
- Template::Declare->buffer->append( $buf);
- return (ref($more_code) && $more_code->isa('CODE')) ? $more_code->() : '';
+ Template::Declare->buffer->append($buf);
+ return ( ref($more_code) && $more_code->isa('CODE') )
+ ? $more_code->()
+ : '';
}
=head2 show [$template_name or $template_coderef]
@@ -426,8 +430,7 @@
# if we're inside a template, we should show private templates
if ( caller->isa('Template::Declare') ) {
$INSIDE_TEMPLATE = 1;
- }
- else {
+ } else {
Template::Declare->new_buffer_frame;
}
_show_template( $template, $INSIDE_TEMPLATE );
@@ -441,7 +444,7 @@
}
sub show_page {
- my $template = shift;
+ my $template = shift;
my $INSIDE_TEMPLATE = 0;
# if we're inside a template, we should show private templates
@@ -449,14 +452,14 @@
_show_template( $template, 0 );
my $data = Template::Declare->buffer->data;
Template::Declare->end_buffer_frame;
- Template::Declare->buffer->append($data);
- %ELEMENT_ID_CACHE = (); # We're done. we can clear the cache
+ Template::Declare->buffer->append($data);
+ %ELEMENT_ID_CACHE = (); # We're done. we can clear the cache
return $data;
}
sub _show_template {
- my $template = shift;
- my $INSIDE_TEMPLATE = shift;
+ my $template = shift;
+ my $INSIDE_TEMPLATE = shift;
my $callable =
( ref($template) && $template->isa('Template::Declare::Tag') )
@@ -478,6 +481,7 @@
Template::Declare->buffer->append($content);
}
+
sub _escape_utf8 {
my $val = shift;
no warnings 'uninitialized';
@@ -514,7 +518,8 @@
=cut
sub Tr (&) {
- die "Tr {...} and td {...} are invalid; use row {...} and cell {...} instead.";
+ die
+ "Tr {...} and td {...} are invalid; use row {...} and cell {...} instead.";
}
=head2 td
@@ -525,10 +530,10 @@
=cut
sub td (&) {
- die "Tr {...} and td {...} are invalid; use row {...} and cell {...} instead.";
+ die
+ "Tr {...} and td {...} are invalid; use row {...} and cell {...} instead.";
}
-
=head1 SEE ALSO
L<Template::Declare>
@@ -543,7 +548,6 @@
=cut
-
package Template::Declare::Tag;
use overload '""' => \&stringify;
@@ -551,18 +555,16 @@
sub stringify {
my $self = shift;
- if (defined wantarray) {
- Template::Declare->new_buffer_frame;
- my $returned =$self->();
- my $content = Template::Declare->buffer->data();
- Template::Declare->end_buffer_frame;
- return ($content . $returned);
+ if ( defined wantarray ) {
+ Template::Declare->new_buffer_frame;
+ my $returned = $self->();
+ my $content = Template::Declare->buffer->data();
+ Template::Declare->end_buffer_frame;
+ return ( $content . $returned );
} else {
return $self->();
}
}
-
-
1;
More information about the Jifty-commit
mailing list