[Jifty-commit] r3602 - in Template-Declare: lib/Template
lib/Template/Declare t
jifty-commit at lists.jifty.org
jifty-commit at lists.jifty.org
Mon Jul 2 13:35:01 EDT 2007
Author: sartak
Date: Mon Jul 2 13:35:01 2007
New Revision: 3602
Added:
Template-Declare/t/arg-passing.t
Modified:
Template-Declare/ (props changed)
Template-Declare/lib/Template/Declare.pm
Template-Declare/lib/Template/Declare/Tags.pm
Log:
r21149 at caladan: sartak | 2007-07-01 14:56:00 -0400
local copy of TD
r21197 at caladan: sartak | 2007-07-02 13:03:29 -0400
Add (simple) tests for passing arguments to templates
r21198 at caladan: sartak | 2007-07-02 13:06:39 -0400
Make tests pass by changing how arguments are passed to top-level templates
Before they were inconsistently passed in an arrayref (where intra-template arguments were passed in simple arrays)
This may break apps! But we don't document passing args (in fact in Template::Declare::tags there is a comment saying *not* to pass args)
r21199 at caladan: sartak | 2007-07-02 13:34:48 -0400
Update documentation to show argument passing.
Modified: Template-Declare/lib/Template/Declare.pm
==============================================================================
--- Template-Declare/lib/Template/Declare.pm (original)
+++ Template-Declare/lib/Template/Declare.pm Mon Jul 2 13:35:01 2007
@@ -95,7 +95,7 @@
=head2 A slightly more advanced example
-In this example, we'll show off how to set attributes on HTML tags, how to call other templates and how to declare a I<private> template that can't be called directly.
+In this example, we'll show off how to set attributes on HTML tags, how to call other templates and how to declare a I<private> template that can't be called directly. We'll also show passing arguments to templates.
package MyApp::Templates;
use Template::Declare::Tags;
@@ -108,20 +108,33 @@
}
};
+ private template 'footer' => sub {
+ my $self = shift;
+ my $time = shift || gmtime;
+
+ div { attr { id => "footer"};
+ "Page last generated at $time."
+ }
+ };
+
template simple => sub {
+ my $self = shift;
+ my $user = shift || 'world wide web';
+
html {
show('header');
body {
p { attr { class => 'greeting'};
- 'Hello, world wide web!'};
- }
+ "Hello, $user!"};
+ };
+ show('footer');
}
};
package main;
use Template::Declare;
Template::Declare->init( roots => ['MyApp::Templates']);
- print Template::Declare->show( 'simple');
+ print Template::Declare->show( 'simple', 'TD user');
# Output:
#
@@ -132,9 +145,10 @@
# <meta generator="This is not your father's frontpage" />
# </head>
# <body>
- # <p class="greeting">Hello, world wide web!
+ # <p class="greeting">Hello, TD user!
# </p>
# </body>
+ # <div id="footer">Page last generated at Mon Jul 2 17:09:34 2007.</div>
# </html>
@@ -203,7 +217,7 @@
my $class = shift;
my $template = shift;
local %Template::Declare::Tags::ELEMENT_ID_CACHE = ();
- return Template::Declare::Tags::show_page($template => \@_);
+ return Template::Declare::Tags::show_page($template => @_);
}
=head2 alias
Modified: Template-Declare/lib/Template/Declare/Tags.pm
==============================================================================
--- Template-Declare/lib/Template/Declare/Tags.pm (original)
+++ Template-Declare/lib/Template/Declare/Tags.pm Mon Jul 2 13:35:01 2007
@@ -399,13 +399,10 @@
: '';
}
-=head2 show [$template_name or $template_coderef]
+=head2 show [$template_name or $template_coderef], args
-C<show> displays templates.
-
-Do not call templates with arguments. That's not supported.
-
-XXX TODO: This makes jesse cry. Audrey/cl: sanity check?
+C<show> displays templates. C<args> will be passed directly to the
+template.
C<show> can either be called with a template name or a package/object
and a template. (It's both functional and OO.)
Added: Template-Declare/t/arg-passing.t
==============================================================================
--- (empty file)
+++ Template-Declare/t/arg-passing.t Mon Jul 2 13:35:01 2007
@@ -0,0 +1,34 @@
+#!/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" }
+};
+
+package main;
+use strict;
+use warnings;
+use Test::More tests => 3;
+use Template::Declare;
+Template::Declare->init(roots => ['MyApp::Templates']);
+
+my $out = Template::Declare->show('inner', 'inside');
+like($out, qr/inner: inside/);
+
+$out = Template::Declare->show('outer', 'xyzzy');
+like($out, qr/outer: xyzzy/);
+like($out, qr/inner: XYZZY/);
+
More information about the Jifty-commit
mailing list