Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Using DateTime together with Template::Toolkit

by Corion (Patriarch)
on Feb 28, 2005 at 21:45 UTC ( [id://435201]=perlquestion: print w/replies, xml ) Need Help??

Corion has asked for the wisdom of the Perl Monks concerning the following question:

I am most likely looking in the wrong places and have missed some crucial piece of information. I can't get DateTime objects to play well with Template::Stash - the Template::Stash seems to want to compare the DateTime object against the template root, but the DateTime object does not want to be compared with anything other than a DateTime object, or something sufficiently similar. See my code below - it is some convoluted way of printing todays date, except it doesn't:

use strict; use DateTime; use Template; my $config = {}; my $template = Template->new($config); my $input = \'[% calendar.ymd(".") %]'; # process input template, substituting variables $template->process($input, { calendar => DateTime->now }) or die $template->error();

After patching DateTime.pm so it gives some better error diagnostics, I get

undef error - Cannot compare a datetime to a regular scalar (2005-02-2 +8T21:51:58 / Template::Stash=HASH(0x2048a14)) at c:/perl/site/5.8.5/l +ib/Template/Stash.pm line 582

but I don't want to believe that nobody has used Template::Toolkit and DateTime together, so the error must be somewhere in my usage of one of the two.

Update: DateTime v0.28, Template::Toolkit 2.14

Update 2: I hacked myself a workaround by removing the overloaded string comparison of DateTime. This is horribly ugly and there must be a better way :-(

Replies are listed 'Best First'.
Re: Using DateTime together with Template::Toolkit
by friedo (Prior) on Feb 28, 2005 at 22:26 UTC
    Update 2: I hacked myself a workaround by removing the overloaded string comparison of DateTime. This is horribly ugly and there must be a better way :-(

    Yes, that way is to not put method calls in templates. Is it really so hard to use the following?

    my $input = \'[% calendar %]'; $template->process($input, { calendar => DateTime->now->ymd(".") } );

    I realize this is just a matter of style, but I really can't think of any good reason for such a simple operation not to occur in your program. Your program should assemble data, and your template should display it.

    (Caveat: For efficiency reasons, sometimes it's better to pass an iterator to a template instead of a fully constructed data structure. But this sort of thing should be the exception, not the rule, IMHO.)

      I can think of a great reason not to do it: datetime formatting is a display issue, not a data issue. They should be kept separate as much as possible otherwise you start doing things like putting your HTML construction alongside your data access. Down that path lies a maintenance nightmare.

      Chris
      M-x auto-bs-mode

      Of course, that could be a way to do it, but then, I like being able to make method/accessor calls on my objects - that's why I use Template::Toolkit and not HTML::Template. And I don't want to touch my precious Perl code just to change the formatting, especially for things as fragile as date separators.

Re: Using DateTime together with Template::Toolkit
by mdillon (Priest) on Mar 01, 2005 at 06:59 UTC
    If you were using the XS Stash, I don't think this would be a problem.

    If you really have a reason to use the non-XS stash, you can patch TT. The fix is to change the code that creates the $atroot variable in the _dotop method. It is described in this post to the TT mailing list. There is similar code in _assign that could probably stand to be patched as well.

    Note: The change in question adds a dependency on the Scalar::Util module, which should work on whatever version of Perl you can run TT on.

      I don't know resp. didn't check if the XS Stash is active or not, but it sure sounds like if it isn't. The patch you mentioned sounds promising - I will try that!

      Of course, there shouldn't be a difference between the XS version and the Pure Perl version of Template::Stash.

        I agree that they should work the same, but in this case, there is a discrepency.

        I only suggested using the XS Stash instead since it seems to work correctly. Also, that message was posted to the TT mailing list a few months ago (in response to a message from the maintainer, so he's obviously aware of it) and there hasn't been a release containing the fix yet. That makes me think that waiting for the pure Perl Stash to work may take a while.

        Why not? They do different things ...

        Being right, does not endow the right to be rude; politeness costs nothing.
        Being unknowing, is not the same as being stupid.
        Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
        Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re: Using DateTime together with Template::Toolkit
by mpeters (Chaplain) on Feb 28, 2005 at 21:58 UTC
    That code snippet works just fine for me.
    DateTime 0.23
    Template 2.14

    Update: It also worked for me for DateTime 0.28
Re: Using DateTime together with Template::Toolkit
by lachoy (Parson) on Feb 28, 2005 at 23:06 UTC
    Your code worked fine for me also: TT 2.10 and DateTime 0.20. (Wow, I gotta upgrade!)

    update: after upgrading to 2.14 and 0.28 it still worked okay. Did you build and are you using the XS TT stash?

    Chris
    M-x auto-bs-mode

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://435201]
Approved by blokhead
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-18 15:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found