[Jifty-commit] r5645 - in Template-Declare: lib/Template/Declare t
Jifty commits
jifty-commit at lists.jifty.org
Fri Aug 1 17:16:18 EDT 2008
Author: sartak
Date: Fri Aug 1 17:16:17 2008
New Revision: 5645
Added:
Template-Declare/t/wrappers.t
Modified:
Template-Declare/ (props changed)
Template-Declare/lib/Template/Declare/Tags.pm
Template-Declare/t/trivial.t
Log:
r68311 at onn: sartak | 2008-08-01 17:16:02 -0400
Sugar for defining a tag-like wrapper! Theory++
Resolves rt.cpan.org #37624
Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm (original)
+++ Template-Declare/lib/Template/Declare/Tags.pm Fri Aug 1 17:16:17 2008
@@ -18,7 +18,7 @@
= qw( with template private show show_page attr outs
outs_raw in_isolation $self under
get_current_attr xml_decl
- smart_tag_wrapper current_template );
+ smart_tag_wrapper current_template wrapper );
our @TAG_SUB_LIST;
*TagSubs = \@TAG_SUB_LIST; # For backward compatibility only
@@ -273,6 +273,65 @@
}
+=head2 wrapper WRAPPERNAME => sub { 'Implementation' };
+
+C<wrapper> declares a wrapper subroutine that can be called like a tag sub,
+but can optionally take arguments to be passed to the wrapper sub. For
+example, if you wanted to wrap all of the output of a template in the usual
+HTML headers and footers, you can do something like this:
+
+ package MyApp::Templates;
+ use Template::Declare::Tags;
+ use base 'Template::Declare';
+
+ BEGIN {
+ wrapper wrap => sub {
+ my $code = shift;
+ my %params = @_;
+ html {
+ head { title { outs "Hello, $params{user}!"} };
+ body {
+ $code->();
+ div { outs 'This is the end, my friend' };
+ };
+ }
+ };
+ }
+
+ template inner => sub {
+ wrap {
+ h1 { outs "Hello, Jesse, s'up?" };
+ } user => 'Jesse';
+ };
+
+Note how the C<wrap> wrapper function is available for calling after it has
+been declared in a C<BEGIN> block. Also note how you can pass arguments to the
+function after the closing brace (you don't need a comma there!).
+
+The output from the "inner" template will look something like this:
+
+ <html>
+ <head>
+ <title>Hello, Jesse!</title>
+ </head>
+ <body>
+ <h1>Hello, Jesse, s'up?</h1>
+ <div>This is the end, my friend</div>
+ </body>
+ </html>
+
+=cut
+
+sub wrapper ($$) {
+ my $wrapper_name = shift;
+ my $coderef = shift;
+ my $template_class = caller;
+
+ # Shove the code ref into the calling class.
+ no strict 'refs';
+ *{"$template_class\::$wrapper_name"} = sub (&;@) { goto $coderef };
+}
+
=head2 private template TEMPLATENAME => sub { 'Implementation' };
C<private> declares that a template isn't available to be called directly from client code.
Modified: Template-Declare/t/trivial.t
==============================================================================
--- Template-Declare/t/trivial.t (original)
+++ Template-Declare/t/trivial.t Fri Aug 1 17:16:17 2008
@@ -26,7 +26,7 @@
};
-sub wrapper {
+sub wrap {
my ( $title, $coderef) = (@_);
outs_raw '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
with ( xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => "en"),
@@ -45,7 +45,7 @@
template markup => sub {
my $self = shift;
- wrapper(
+ wrap(
'My page!',
sub {
Added: Template-Declare/t/wrappers.t
==============================================================================
--- (empty file)
+++ Template-Declare/t/wrappers.t Fri Aug 1 17:16:17 2008
@@ -0,0 +1,46 @@
+#!/usr/bin/perl
+package MyApp::Templates;
+use strict;
+use warnings;
+use Template::Declare::Tags;
+use base 'Template::Declare';
+
+BEGIN {
+ wrapper wrap => sub {
+ my $code = shift;
+ my %params = @_;
+ html {
+ head { title { outs "Hello, $params{user}!"} };
+ body {
+ $code->();
+ div { outs 'This is the end, my friend' };
+ };
+ }
+ };
+}
+
+template inner => sub {
+ wrap {
+ h1 { outs "Hello, Jesse, s'up?" };
+ } user => 'Jesse';
+};
+
+package main;
+use strict;
+use warnings;
+use Test::More tests => 2;
+use Template::Declare;
+Template::Declare->init(roots => ['MyApp::Templates']);
+
+ok my $out = Template::Declare->show('inner'), 'Get inner output';
+is $out, '
+<html>
+ <head>
+ <title>Hello, Jesse!</title>
+ </head>
+ <body>
+ <h1>Hello, Jesse, s'up?</h1>
+ <div>This is the end, my friend</div>
+ </body>
+</html>', 'Should have the wrapped output';
+
More information about the Jifty-commit
mailing list