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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Feb 5 23:14:34 EST 2007


Author: jesse
Date: Mon Feb  5 23:14:33 2007
New Revision: 2754

Added:
   Template-Declare/lib/Template/Declare/Buffer.pm
Modified:
   Template-Declare/   (props changed)
   Template-Declare/META.yml
   Template-Declare/lib/Template/Declare.pm
   Template-Declare/lib/Template/Declare/Tags.pm
   Template-Declare/t/aliasing.t
   Template-Declare/t/arg-declaration-styles.t
   Template-Declare/t/importing.t
   Template-Declare/t/indexhtml.t
   Template-Declare/t/private.t
   Template-Declare/t/self.t
   Template-Declare/t/subclassing.t
   Template-Declare/t/subtemplates.t
   Template-Declare/t/trivial.t
   Template-Declare/t/xss.t

Log:
 r21797 at hualien:  jesse | 2007-02-06 17:11:57 +1300
  * Switched from Template::Declare::BUFFER to a buffer object


Modified: Template-Declare/META.yml
==============================================================================
--- Template-Declare/META.yml	(original)
+++ Template-Declare/META.yml	Mon Feb  5 23:14:33 2007
@@ -1,14 +1,15 @@
---- 
-abstract: ~
+abstract: Perlish declarative templates
 author: Jesse Vincent <jesse at bestpractical.com>
 build_requires: 
   HTML::Lint: 0
 distribution_type: module
 generated_by: Module::Install version 0.64
-license: Perl
+license: perl
 name: Template-Declare
 no_index: 
   directory: 
     - inc
     - t
+requires: 
+  perl: 5.6.0
 version: 0.02

Modified: Template-Declare/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/lib/Template/Declare.pm	(original)
+++ Template-Declare/lib/Template/Declare.pm	Mon Feb  5 23:14:33 2007
@@ -4,6 +4,7 @@
 use Carp;
 
 package Template::Declare;
+use Template::Declare::Buffer;
 
 $Template::Declare::VERSION = "0.02";
 
@@ -13,6 +14,7 @@
 __PACKAGE__->mk_classdata('alias_prefixes');
 __PACKAGE__->mk_classdata('templates');
 __PACKAGE__->mk_classdata('private_templates');
+__PACKAGE__->mk_classdata('buffer_stack');
 
 
 __PACKAGE__->roots([]);
@@ -20,7 +22,9 @@
 __PACKAGE__->alias_prefixes({});
 __PACKAGE__->templates({});
 __PACKAGE__->private_templates({});
+__PACKAGE__->buffer_stack([]);
 
+__PACKAGE__->new_buffer_frame();
 
 
 =head1 NAME
@@ -162,19 +166,44 @@
 
 }
 
+
+sub new_buffer_frame {
+    my $buffer = Template::Declare::Buffer->new();
+    unshift @{__PACKAGE__->buffer_stack}, $buffer;
+
+}
+
+sub end_buffer_frame {
+    shift @{__PACKAGE__->buffer_stack};
+}
+
+sub buffer {
+    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 render that template and return the content as a scalar.
+Call C<show> with a C<template_name> and C<Template::Declare> will
+render that template. Content generated by show can be accessed with
+the C<output> method if the output method you've chosen returns content
+instead of outputting it directly.
+
+(If called in scalar context, this method will also just return the
+content when available).
+
+
 
 =cut
 
 sub show {
     my $class = shift;
     my $template = shift;
-    Template::Declare::Tags::show($template);
-
+    return Template::Declare::Tags::show($template);
 }
 
+
 =head2 alias
 
  alias Some::Clever::Mixin under '/mixin';

Added: Template-Declare/lib/Template/Declare/Buffer.pm
==============================================================================
--- (empty file)
+++ Template-Declare/lib/Template/Declare/Buffer.pm	Mon Feb  5 23:14:33 2007
@@ -0,0 +1,24 @@
+use warnings;
+use strict;
+
+package Template::Declare::Buffer;
+use base 'Class::Accessor';
+
+__PACKAGE__->mk_accessors('data');
+
+
+
+
+sub append {
+    my $self = shift;
+    my $content = shift;
+    $self->data(($self->data||'').$content);
+}
+
+sub clear {
+    my $self = shift;
+    $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	Mon Feb  5 23:14:33 2007
@@ -12,7 +12,6 @@
 push @EXPORT, qw(Tr td );    # these two warns the user to use row/cell instead
 
 our %ATTRIBUTES = ();
-our $BUFFER     = '';
 our $DEPTH      = 0;
 
 
@@ -118,7 +117,7 @@
 =cut
 
 sub outs_raw {
-    $BUFFER .= join( '', grep {defined} @_ );
+    Template::Declare->buffer->append( join( '', grep {defined} @_ ));
     return '';
 }
 
@@ -262,15 +261,15 @@
             wantarray ? () : '';
         };
 
-        local $BUFFER;
         local $DEPTH = $DEPTH + 1;
         %ATTRIBUTES = ();
+        Template::Declare->new_buffer_frame;
         my $last = join '', map { ref($_) ? $_ : _escape_utf8($_) } $code->();
 
-        if ( length($BUFFER) ) {
+        if ( length(Template::Declare->buffer->data) ) {
 
-# We concatenate to force scalarization when $last or $BUFFER is solely a Jifty::Web::Link
-            $buf .= '>' . $BUFFER;
+# We concatenate to force scalarization when $last or $Template::Declare->buffer is solely a Jifty::Web::Link
+            $buf .= '>' . Template::Declare->buffer->data;
             $had_content = 1;
         } elsif ( length $last ) {
             $buf .= '>' . $last;
@@ -278,23 +277,27 @@
         } else {
             $had_content = 0;
         }
+        
+        Template::Declare->end_buffer_frame;
 
     }
 
     if ($had_content) {
-        $BUFFER .= $buf;
-        $BUFFER .= "\n" . ( " " x $DEPTH ) if ( $buf =~ /\n/ );
-        $BUFFER .= "</$tag>";
+        $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) {
         # XXX TODO: This should come out of HTML::Tagset
         # EMPTY tags can close themselves.
-        $BUFFER .= $buf . " />";
+         $buf .= " />";
     } else {
 
         # Otherwise we supply a closing tag.
-        $BUFFER .= $buf . "></$tag>";
+        $buf .= "></$tag>";
     }
 
+
+    Template::Declare->buffer->append( $buf);
+
     return $more_code ? $more_code->() : '';
 }
 
@@ -323,6 +326,7 @@
 
     # if we're inside a template, we should show private templates
     if ( caller()->isa('Template::Declare') ) { $INSIDE_TEMPLATE = 1; }
+    else { Template::Declare->new_buffer_frame }
     my $callable =
         ref($template) eq 'CODE'
         ? $template
@@ -330,15 +334,14 @@
 
     return '' unless ($callable);
 
-    my $buf = '';
-    {
-        local $BUFFER = '';
+        Template::Declare->new_buffer_frame;
         &$callable($self);
-        $buf = $BUFFER;
-    }
-
-    $BUFFER .= $buf;
-    return $buf;
+        my $content = Template::Declare->buffer->data;
+        Template::Declare->end_buffer_frame;
+        Template::Declare->buffer->append( $content);
+    my $data =  Template::Declare->buffer->data;
+    unless ($INSIDE_TEMPLATE) {  Template::Declare->end_buffer_frame  }
+    return $data;
 }
 
 sub _escape_utf8 {

Modified: Template-Declare/t/aliasing.t
==============================================================================
--- Template-Declare/t/aliasing.t	(original)
+++ Template-Declare/t/aliasing.t	Mon Feb  5 23:14:33 2007
@@ -55,7 +55,6 @@
 ok( Template::Declare->has_template('aliased_subclass_pkg/aliased'), "When you subclass and then alias, the superclass's aliass are there" );
 
 {
-    local $Template::Declare::Tags::BUFFER;
     local $Template::Declare::Tags::self = 'Wifty::UI';
     my $simple = ( show('aliased_pkg/aliased') );
     like( $simple, qr'This is aliased' );
@@ -66,7 +65,6 @@
 
 
 {
-    local $Template::Declare::Tags::BUFFER;
     local $Template::Declare::Tags::self = 'Wifty::UI';
     my $simple = ( show('aliased_subclass_pkg/aliased') );
     like(

Modified: Template-Declare/t/arg-declaration-styles.t
==============================================================================
--- Template-Declare/t/arg-declaration-styles.t	(original)
+++ Template-Declare/t/arg-declaration-styles.t	Mon Feb  5 23:14:33 2007
@@ -69,7 +69,6 @@
 Template::Declare->init(roots => ['TestApp::UI']);
 
 for (qw(content content_curly content_explicit)) {
-    local $Template::Declare::Tags::BUFFER;
     ok_content( show($_), $_ );
 }
 
@@ -77,7 +76,6 @@
     qw(content_mixed1 content_mixed2 content_attrs content_withs content_curlies)
   )
 {
-    local $Template::Declare::Tags::BUFFER;
     ok_multicontent( show($_), $_ );
 }
 

Modified: Template-Declare/t/importing.t
==============================================================================
--- Template-Declare/t/importing.t	(original)
+++ Template-Declare/t/importing.t	Mon Feb  5 23:14:33 2007
@@ -51,7 +51,6 @@
 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;
     local $Template::Declare::Tags::self = 'Wifty::UI';
     my $simple = ( show('imported_pkg/imported') );
     like( $simple, qr'This is imported' );
@@ -61,7 +60,6 @@
 }
 
 {
-    local $Template::Declare::Tags::BUFFER;
     local $Template::Declare::Tags::self = 'Wifty::UI';
     my $simple = ( show('imported_subclass_pkg/imported') );
     like(

Modified: Template-Declare/t/indexhtml.t
==============================================================================
--- Template-Declare/t/indexhtml.t	(original)
+++ Template-Declare/t/indexhtml.t	Mon Feb  5 23:14:33 2007
@@ -45,7 +45,7 @@
 
 for('index.html', 'dash-test'){ 
 {
-local $Template::Declare::Tags::BUFFER;
+Template::Declare->buffer->clear;
 my $simple =(show($_));
 ok($simple =~ 'This is my content');
 #diag ($simple);

Modified: Template-Declare/t/private.t
==============================================================================
--- Template-Declare/t/private.t	(original)
+++ Template-Declare/t/private.t	Mon Feb  5 23:14:33 2007
@@ -39,20 +39,17 @@
 require "t/utils.pl";
 
 {
-    local $Template::Declare::Tags::BUFFER;
     my $simple = ( show('simple') );
    like( $simple,  qr'This is my content' );
    like( $simple,  qr'This is other 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);

Modified: Template-Declare/t/self.t
==============================================================================
--- Template-Declare/t/self.t	(original)
+++ Template-Declare/t/self.t	Mon Feb  5 23:14:33 2007
@@ -26,7 +26,6 @@
 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' );

Modified: Template-Declare/t/subclassing.t
==============================================================================
--- Template-Declare/t/subclassing.t	(original)
+++ Template-Declare/t/subclassing.t	Mon Feb  5 23:14:33 2007
@@ -55,7 +55,6 @@
 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' );
@@ -67,20 +66,17 @@
 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);

Modified: Template-Declare/t/subtemplates.t
==============================================================================
--- Template-Declare/t/subtemplates.t	(original)
+++ Template-Declare/t/subtemplates.t	Mon Feb  5 23:14:33 2007
@@ -31,14 +31,12 @@
 
 
 {
-local $Template::Declare::Tags::BUFFER;
 my $simple =(show('my/content'));
 ok($simple =~ 'This is my content');
 #diag ($simple);
 ok_lint($simple);
 }
 {
-local $Template::Declare::Tags::BUFFER;
 my $simple =(show('simple'));
 ok($simple =~ 'This is my content');
 #diag ($simple);

Modified: Template-Declare/t/trivial.t
==============================================================================
--- Template-Declare/t/trivial.t	(original)
+++ Template-Declare/t/trivial.t	Mon Feb  5 23:14:33 2007
@@ -5,7 +5,7 @@
 package Wifty::UI;
 use base qw/Template::Declare/;
 use Template::Declare::Tags;
-use Test::More tests => 5;
+use Test::More tests => 9;
 
 template simple => sub {
 
@@ -144,13 +144,28 @@
 Template::Declare->init( roots => ['Wifty::UI']);
 
 {
-local $Template::Declare::Tags::BUFFER;
+Template::Declare->buffer->clear;
 my $simple =(show('simple'));
 ok($simple =~ 'This is my content');
 #diag ($simple);
 ok_lint($simple);
 }
-{local $Template::Declare::Tags::BUFFER;
+{
+Template::Declare->buffer->clear;
+my $simple =Template::Declare->show('simple');
+ok($simple =~ 'This is my content');
+#diag ($simple);
+ok_lint($simple);
+}
+{
+Template::Declare->buffer->clear;
+Template::Declare->show('simple');
+ok(Template::Declare->buffer->data() =~ 'This is my content');
+#diag ($simple);
+ok_lint(Template::Declare->buffer->data());
+}
+{
+Template::Declare->buffer->clear;
 my $out =  (show('markup'));
 #diag($out);
 my @lines = split("\n",$out);

Modified: Template-Declare/t/xss.t
==============================================================================
--- Template-Declare/t/xss.t	(original)
+++ Template-Declare/t/xss.t	Mon Feb  5 23:14:33 2007
@@ -27,7 +27,7 @@
 
 for (qw(content content_2 content_3 ) ){
 {
-local $Template::Declare::Tags::BUFFER;
+Template::Declare->buffer->clear;
 my $simple =(show($_));
 ok($simple =~ 'This is my &lt;b&gt;content');
 ok_lint($simple);
@@ -35,9 +35,9 @@
 }
 for (qw(content_4) ){
 {
-local $Template::Declare::Tags::BUFFER;
+Template::Declare->buffer->clear;
 my $simple =(show($_));
-ok($simple =~ m/This is my\s*<b>\s*content/);
+ok($simple =~ m/This is my\s*<b>\s*content/, $simple);
 ok_lint($simple);
 }
 }


More information about the Jifty-commit mailing list