Subject: | Template::Stash (NoXS) with DateTime objects |
Hi,
here's a patch suggested to allow passing objects with
stringification overloading to Template::Stash, the pure perl version.
This address the annoying issues with DateTime with Template::Stash
without XS support. t/datetime.t is the unit test to verify the fix is
correct.
=== lib/Template/Stash.pm
==================================================================
--- lib/Template/Stash.pm (revision 5347)
+++ lib/Template/Stash.pm (local)
@@ -682,7 +682,7 @@
sub _dotop {
my ($self, $root, $item, $args, $lvalue) = @_;
my $rootref = ref $root;
- my $atroot = ($root eq $self);
+ my $atroot = eval { $root->isa(__PACKAGE__) };
my ($value, @result);
$args ||= [ ];
=== t/datetime.t
==================================================================
--- t/datetime.t (revision 5347)
+++ t/datetime.t (local)
@@ -0,0 +1,15 @@
+use strict;
+use Test::More;
+
+eval { require DateTime };
+plan skip_all => 'DateTime required' if $@;
+
+use Template;
+use Template::Stash;
+
+plan tests => 1;
+
+my $tmpl = "[% date.ymd %]";
+
+my $tt = Template->new({ STASH => Template::Stash->new });
+ok( $tt->process(\$tmpl, { date => DateTime->now }, \my $out) );
Show quoted text
---------- Forwarded message ----------
From: Daisuke Maki <daisuke@endeworks.jp>
Date: Sep 23, 2006 4:29 PM
Subject: Re: Template::Stash + DateTime overloading
To: Tatsuhiko Miyagawa <miyagawa@gmail.com>
Cc: datetime <datetime@perl.org>
Smells like a TT problem.
The line in question at Template::Stash is reads:
my $atroot = ($root eq $self);
This logic exists so such that TT knows if it's looking at the implicit
stash context, or a child element in the stash.
The same logic, if I'm not mistaken, in the XS version reads:
atroot = sv_derived_from(root, TT_STASH_PKG);
So if I change line 685 of Stash.pm to
my $atroot = eval { $root->isa(__PACKAGE__) };
Voila!
daisuke@beefcake daisuke$ perl test.pl
# DateTime 0.34
# Template 2.15
ok 1 - ok
ok 2 - ok
1..2
--d
Tatsuhiko Miyagawa wrote:
> Hi,
>
> Passing DateTime object to TT (Template Toolkit) without Stash::XS
> built, DateTime overloading gives you an annoying error like
> following. (See DATA section for the result on my box.)
>
> Is it something fixable on the user's end, or simply the bug of
> DateTime, or TT?
>
> #!/usr/bin/perl
> use strict;
> use warnings;
> use DateTime;
> use Template;
> use Template::Stash;
> use Template::Stash::XS;
> use Test::More 'no_plan';
>
> diag "DateTime $DateTime::VERSION";
> diag "Template $Template::VERSION";
>
> my $tmpl = <<TMPL;
> today is [% date.ymd %]
> TMPL
>
> for my $stash ( Template::Stash->new, Template::Stash::XS->new ) {
> my $tt = Template->new({ STASH => $stash });
> ok( $tt->process(\$tmpl, { date => DateTime->now }, \my $out),
> $tt->error || "ok");
> }
>
> __END__
> # DateTime 0.30
> # Template 2.14
> not ok 1 - undef error - Cannot compare a datetime to a regular
scalar
> at /usr/local/lib/perl/5.8.4/DateTime.pm line 1435.
> #
> # Failed test 'undef error - Cannot compare a datetime to a regular
> scalar at /usr/local/lib/perl/5.8.4/DateTime.pm line 1435.
> # '
> # in /home/miyagawa/tmp/tt-datetime.pl at line 19.
> ok 2 - ok
> 1..2
> # Looks like you failed 1 test of 2.
>
>