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

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Fri Mar 16 05:15:53 EDT 2007


Author: evdb
Date: Fri Mar 16 05:15:53 2007
New Revision: 2989

Added:
   Template-Declare/t/smart_tag_wrapper.t   (contents, props changed)
Modified:
   Template-Declare/META.yml
   Template-Declare/lib/Template/Declare/Tags.pm

Log:
Added function 'smart_tag_wrapper' that allows tags to be created that have access to the ATTRIBUTES set using 'with'. It attempts to hide most of the complexity of the call from the user and tidies up after itself.


Modified: Template-Declare/META.yml
==============================================================================
--- Template-Declare/META.yml	(original)
+++ Template-Declare/META.yml	Fri Mar 16 05:15:53 2007
@@ -3,6 +3,7 @@
 build_requires: 
   HTML::Lint: 0
   Test::More: 0
+  Test::Warn: 0
 distribution_type: module
 generated_by: Module::Install version 0.61
 license: perl

Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm	(original)
+++ Template-Declare/lib/Template/Declare/Tags.pm	Fri Mar 16 05:15:53 2007
@@ -8,7 +8,7 @@
 use base 'Exporter';
 use Carp;
 
- at EXPORT = qw( with template private show attr outs outs_raw in_isolation $self under get_current_attr );
+ 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 = ();
@@ -217,6 +217,63 @@
     wantarray ? () : '';
 }
 
+=head2 smart_tag_wrapper
+
+  # create a tag that has access to the arguments set with with.
+  sub sample_smart_tag (&) {
+      my $code = shift;
+
+      smart_tag_wrapper {
+          my %args = @_; # set using 'with'
+          outs( 'keys: ' . join( ', ', sort keys %args) . "\n" );
+          $code->();
+      };
+  }
+  
+  # use it
+  with ( foo => 'bar', baz => 'bundy' ),
+    sample_smart_tag {
+      outs( "Hello, World!\n" );
+    };
+
+  # output would be
+  keys: baz, foo
+  Hello, World!
+
+The smart tag wrapper allows you to create code that has access to the arguments
+set using 'with', it passes them in to the wrapped code in C<@_>. It also takes
+care of putting the output in the right place and tidying up after itself.
+
+=cut
+
+sub smart_tag_wrapper (&) {
+    my $coderef = shift;
+    my $buf     = "\n";
+
+    Template::Declare->new_buffer_frame;
+
+    my $last = join '',    #
+      map { ref($_) ? $_ : _escape_utf8($_) }    #
+      $coderef->(%ATTRIBUTES);
+
+    %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 ) {
+        $buf .= $last;
+    }
+
+    Template::Declare->end_buffer_frame;
+    Template::Declare->buffer->append($buf);
+
+    return '';
+}
+
 sub _tag {
     my $code      = shift;
     my $more_code = shift;

Added: Template-Declare/t/smart_tag_wrapper.t
==============================================================================
--- (empty file)
+++ Template-Declare/t/smart_tag_wrapper.t	Fri Mar 16 05:15:53 2007
@@ -0,0 +1,52 @@
+use warnings;
+use strict;
+
+package Wifty::UI;
+use base qw/Template::Declare/;
+use Template::Declare::Tags;
+
+sub test_smart_tag (&) {
+    my $code = shift;
+
+    smart_tag_wrapper {
+        my %args = @_;
+        outs(   "START "
+              . join( ', ', map { "$_: $args{$_}" } sort keys %args )
+              . "\n" );
+        $code->();
+        outs("END\n");
+    };
+}
+
+template simple => sub {
+    with( foo => 'bar' ),    #
+      test_smart_tag { outs("simple\n"); };
+};
+
+template leak_check => sub {
+    with( foo => 'bar' ),    #
+      test_smart_tag { outs("first\n"); };
+    test_smart_tag   { outs("second\n"); };
+};
+
+package main;
+use Template::Declare::Tags;
+Template::Declare->init( roots => ['Wifty::UI'] );
+
+use Test::More tests => 2;
+require "t/utils.pl";
+
+my $simple = show('simple');
+is(
+    $simple,
+    "\nSTART foo: bar\nsimple\nEND\n",
+    "got correct output for simple"
+);
+
+my $leak_check = show('leak_check');
+is(
+    $leak_check,                        #
+    "\nSTART foo: bar\nfirst\nEND\n"    #
+      . "\nSTART \nsecond\nEND\n",      #
+    "got correct output for simple"
+);


More information about the Jifty-commit mailing list