[Jifty-commit] r4904 - 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:31:45 EST 2008
Author: sartak
Date: Tue Jan 22 16:31:45 2008
New Revision: 4904
Modified:
Template-Declare/ (props changed)
Template-Declare/lib/Template/Declare.pm
Template-Declare/lib/Template/Declare/Tags.pm
Template-Declare/t/instrumentation.t
Log:
r50673 at onn: sartak | 2008-01-22 16:31:34 -0500
You've been Moose-ing too long when you start to think in terms of "around" instead of "before" and "after"
So, around_template instead of pre_ and post_. Far less bookkeeping (and chances to screw up) this way.
Modified: Template-Declare/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/lib/Template/Declare.pm (original)
+++ Template-Declare/lib/Template/Declare.pm Tue Jan 22 16:31:45 2008
@@ -18,8 +18,7 @@
__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__->mk_classdata('around_template');
__PACKAGE__->roots( [] );
__PACKAGE__->postprocessor( sub { return wantarray ? @_ : $_[0] } );
@@ -28,8 +27,7 @@
__PACKAGE__->templates( {} );
__PACKAGE__->private_templates( {} );
__PACKAGE__->buffer_stack( [] );
-__PACKAGE__->pre_template( undef );
-__PACKAGE__->post_template( undef );
+__PACKAGE__->around_template( undef );
__PACKAGE__->new_buffer_frame();
@@ -288,15 +286,19 @@
A coderef called to postprocess the HTML or XML output of your templates. This
is to alleviate using Tags for simple text markup.
-=item pre_template
+=item around_template
-A coderef called just before rendering each template. You can use this for
-instrumentation.
-
-=item post_template
-
-A coderef called just after rendering each template. You can use this for
-instrumentation.
+A coderef called B<instead> of rendering each template. The coderef will
+receive three arguments: a coderef to invoke to render the template, the
+template's path, and a reference to the arguments to the template. You can use
+this for instrumentation. For example:
+
+ Template::Declare->init(around_template => sub {
+ my ($orig, $path, $args) = @_;
+ my $start = time;
+ $orig->();
+ warn "Rendering $path took " . (time - $start) . " seconds.";
+ });
=back
@@ -314,12 +316,8 @@
$class->postprocessor( $args{'postprocessor'} );
}
- if ( $args{'pre_template'} ) {
- $class->pre_template( $args{'pre_template'} );
- }
-
- if ( $args{'post_template'} ) {
- $class->post_template( $args{'post_template'} );
+ if ( $args{'around_template'} ) {
+ $class->around_template( $args{'around_template'} );
}
}
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:31:45 2008
@@ -759,14 +759,15 @@
return '';
}
- if (my $instrumentation = Template::Declare->pre_template) {
- $instrumentation->($template, $args);
+ if (my $instrumentation = Template::Declare->around_template) {
+ $instrumentation->(
+ sub { &$callable($self, @$args) },
+ $template,
+ $args,
+ );
}
-
- &$callable($self, @$args);
-
- if (my $instrumentation = Template::Declare->post_template) {
- $instrumentation->($template, $args);
+ else {
+ &$callable($self, @$args);
}
return;
Modified: Template-Declare/t/instrumentation.t
==============================================================================
--- Template-Declare/t/instrumentation.t (original)
+++ Template-Declare/t/instrumentation.t Tue Jan 22 16:31:45 2008
@@ -31,70 +31,54 @@
package main;
use strict;
use warnings;
-use Test::More tests => 37;
+use Test::More tests => 25;
use Template::Declare;
-my @pre;
-my @post;
+my @args;
Template::Declare->init(
roots => ['MyApp::Templates'],
- pre_template => sub { push @pre, [@_] },
- post_template => sub { push @post, [@_] },
+ around_template => sub { push @args, [@_]; shift->() },
);
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 = ();
+is(@args, 1, "one template called");
+is(ref($args[0][0]), 'CODE', "first argument is \$orig");
+is($args[0][1], 'inner', "first argument is template path");
+is_deeply($args[0][2], ['inside'], "second argument is the list of arguments");
+ at args = ();
$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 = ();
+is(@args, 2, "one pre_template called");
+is(ref($args[0][0]), 'CODE', "first argument is \$orig");
+is($args[0][1], 'outer', "nested templates");
+is($args[1][1], 'inner', "nested templates)");
+is_deeply($args[0][2], ['xyzzy'], "nested templates");
+is_deeply($args[1][2], ['XYZZY'], "nested templates");
+ at args = ();
$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 = ();
+is(@args, 1, "one template called");
+is(ref($args[0][0]), 'CODE', "first argument is \$orig");
+is($args[0][1], 'add', "first argument is template path");
+is_deeply($args[0][2], [32, 56], "second argument is the list of arguments");
+ at args = ();
$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");
+is(@args, 2, "one template called");
+is(ref($args[0][0]), 'CODE', "first argument is \$orig");
+is($args[0][1], 'host', "first argument is template path");
+is($args[1][1], 'add', "first argument is template path");
+is_deeply($args[0][2], [], "second argument is the list of arguments");
+is_deeply($args[1][2], [3, 7], "second argument is the list of arguments");
+ at args = ();
+
More information about the Jifty-commit
mailing list