[Jifty-commit] r4901 - in Template-Declare: lib/Template
lib/Template/Declare t
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Tue Jan 22 16:01:39 EST 2008
Author: sartak
Date: Tue Jan 22 16:01:37 2008
New Revision: 4901
Added:
Template-Declare/t/instrumentation.t
Modified:
Template-Declare/ (props changed)
Template-Declare/lib/Template/Declare.pm
Template-Declare/lib/Template/Declare/Tags.pm
Log:
r50669 at onn: sartak | 2008-01-22 16:01:05 -0500
Add pre_template and post_template callbacks for instrumenting your templates
Modified: Template-Declare/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/lib/Template/Declare.pm (original)
+++ Template-Declare/lib/Template/Declare.pm Tue Jan 22 16:01:37 2008
@@ -18,6 +18,8 @@
__PACKAGE__->mk_classdata('private_templates');
__PACKAGE__->mk_classdata('buffer_stack');
__PACKAGE__->mk_classdata('imported_into');
+__PACKAGE__->mk_classdata('pre_template');
+__PACKAGE__->mk_classdata('post_template');
__PACKAGE__->roots( [] );
__PACKAGE__->postprocessor( sub { return wantarray ? @_ : $_[0] } );
@@ -26,6 +28,8 @@
__PACKAGE__->templates( {} );
__PACKAGE__->private_templates( {} );
__PACKAGE__->buffer_stack( [] );
+__PACKAGE__->pre_template( undef );
+__PACKAGE__->post_template( undef );
__PACKAGE__->new_buffer_frame();
@@ -295,6 +299,14 @@
$class->postprocessor( $args{'postprocessor'} );
}
+ if ( $args{'pre_template'} ) {
+ $class->pre_template( $args{'pre_template'} );
+ }
+
+ if ( $args{'post_template'} ) {
+ $class->post_template( $args{'post_template'} );
+ }
+
}
sub new_buffer_frame {
Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm (original)
+++ Template-Declare/lib/Template/Declare/Tags.pm Tue Jan 22 16:01:37 2008
@@ -759,8 +759,16 @@
return '';
}
+ if (my $instrumentation = Template::Declare->pre_template) {
+ $instrumentation->($template, $args);
+ }
&$callable($self, @$args);
+
+ if (my $instrumentation = Template::Declare->post_template) {
+ $instrumentation->($template, $args);
+ }
+
return;
}
Added: Template-Declare/t/instrumentation.t
==============================================================================
--- (empty file)
+++ Template-Declare/t/instrumentation.t Tue Jan 22 16:01:37 2008
@@ -0,0 +1,100 @@
+#!/usr/bin/perl
+package MyApp::Templates;
+use strict;
+use warnings;
+use Template::Declare::Tags;
+use base 'Template::Declare';
+
+template inner => sub {
+ my ($self, $arg) = @_;
+
+ div { "inner: $arg" }
+};
+
+template outer => sub {
+ my ($self, $arg) = @_;
+
+ show('inner', uc $arg);
+ div { "outer: $arg" }
+};
+
+template add => sub {
+ my ($self, $a, $b) = @_;
+ outs "$a + $b";
+};
+
+template host => sub {
+ my $self = shift;
+ show('add', 3, 7);
+};
+
+package main;
+use strict;
+use warnings;
+use Test::More tests => 37;
+use Template::Declare;
+
+my @pre;
+my @post;
+
+Template::Declare->init(
+ roots => ['MyApp::Templates'],
+ pre_template => sub { push @pre, [@_] },
+ post_template => sub { push @post, [@_] },
+);
+
+my $out = Template::Declare->show('inner', 'inside');
+like($out, qr/inner: inside/);
+is(@pre, 1, "one pre_template called");
+is(@post, 1, "one post_template called");
+is($pre[0][0], 'inner', "first argument is template path");
+is($post[0][0], 'inner', "first argument is template path");
+is_deeply($pre[0][1], ['inside'], "second argument is the list of arguments");
+is_deeply($post[0][1], ['inside'], "second argument is the list of arguments");
+
+ at pre = @post = ();
+
+$out = Template::Declare->show('outer', 'xyzzy');
+like($out, qr/outer: xyzzy/);
+like($out, qr/inner: XYZZY/);
+
+is(@pre, 2, "one pre_template called");
+is(@post, 2, "one post_template called");
+is($pre[0][0], 'outer', "nested templates");
+is($post[0][0], 'inner', "nested templates)");
+is($pre[1][0], 'inner', "nested templates");
+is($post[1][0], 'outer', "nested templates");
+
+is_deeply($pre[0][1], ['xyzzy'], "nested templates");
+is_deeply($post[0][1], ['XYZZY'], "nested templates");
+is_deeply($pre[1][1], ['XYZZY'], "nested templates");
+is_deeply($post[1][1], ['xyzzy'], "nested templates");
+
+ at pre = @post = ();
+
+$out = Template::Declare->show('add', '32', '56');
+is($out, '32 + 56');
+
+is(@pre, 1, "one pre_template called");
+is(@post, 1, "one post_template called");
+is($pre[0][0], 'add', "first argument is template path");
+is($post[0][0], 'add', "first argument is template path");
+is_deeply($pre[0][1], [32, 56], "second argument is the list of arguments");
+is_deeply($post[0][1], [32, 56], "second argument is the list of arguments");
+
+ at pre = @post = ();
+
+$out = Template::Declare->show('host');
+is($out, '3 + 7');
+
+is(@pre, 2, "one pre_template called");
+is(@post, 2, "one post_template called");
+is($pre[0][0], 'host', "nested templates");
+is($post[0][0], 'add', "nested templates)");
+is($pre[1][0], 'add', "nested templates");
+is($post[1][0], 'host', "nested templates");
+
+is_deeply($pre[0][1], [], "nested templates");
+is_deeply($post[0][1], [3, 7], "nested templates");
+is_deeply($pre[1][1], [3, 7], "nested templates");
+is_deeply($post[1][1], [], "nested templates");
More information about the Jifty-commit
mailing list