13 Aug 2021 13:31:41 UTC
- Distribution: Template-Plugin-AutoDate
- Module version: 0.90
- Source (raw)
- Browse (raw)
- How to Contribute
- Testers (259 / 0 / 0)
- KwaliteeBus factor: 1
- % Coverage
- License: perl_5
- Download (11.93KB)
- MetaCPAN Explorer
- Subscribe to distribution
- This version
- Latest version
- Michael Conrad
- COPYRIGHT AND LICENSE
Template::Plugin::AutoDate - Enhance Template Toolkit with easy access to DateTime and DateTime::Format::Flexible
[% USE AutoDate %] Yesterday was [% AutoDate.now.subtract(days => 1).ymd("-") %] The record is from month [% AutoDate.coerce(foo.bar.datestring).month_name %] [% USE y2k= AutoDate(year => 2000, month => 1, day => 1) %] [% datestring= "2016-01-01" %] [% datestring.strftime("%M/%d") %] [% datestring.coerce_date.year %]
This module allows you to access the full power of DateTime from within Template Toolkit. Since you don't always have date objects in the data, it also allows you to coerce arbitrary strings into DateTime using DateTime::Format::Flexible.
When you use this plugin, it installs two vmethods into your current Template context:
This can be called on any scalar, and it will parse the scalar with DateTime::Format::Flexible. It returns
undefif the string cannot be parsed, allowing you to continue chaining calls on it and get TT's behavior for undefined values.
If called on an actual DateTime object, it returns the DateTime object un-altered.
When called on a scalar, this coerces it to a DateTime, and if defined, then calls the strftime method on it. This means you can now call
strftimeon any date field you like regardless of whether it's been inflated to a DateTime object by your controller.
It also provides an object (which inherits from DateTime) which you can call methods on.
[% USE AutoDate %] [% USE x = AutoDate(@args) %]
The first form of using the AutoDate module gives you a variable named Autodate which is a subclass of DateTime containing the value of 'now'. (Since it is a DateTime, you may modify its contents! so there is no guarantee that it still holds the value of 'now'.) The primary purpose here is to be able to call the static class methods of DateTime, which can be called on an object just as well as a package name. (Template toolkit plugins do not have the option of returning a package name, and must return a blessed object.)
In the second form, it calls the DateTime constructor with the arguments of your choice, returning a named date object.
[% AutoDate.coerce("January 1, 2000") %]
This class method is a shortcut to "parse_datetime" in DateTime::Format::Flexible. Returns undef if the date can't be parsed.
[% AutoDate.now_local %]
DateTime->now(time_zone => "local")
[% AutoDate.now_floating %]
DateTime->now(time_zone => "local")->set_time_zone("floating"), which results in a floating DateTime that contains local time.
Michael Conrad <firstname.lastname@example.org>
This software is copyright (c) 2021 by IntelliTree Solutions llc.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
Module Install Instructions
To install Template::Plugin::AutoDate, copy and paste the appropriate command in to your terminal.
perl -MCPAN -e shell install Template::Plugin::AutoDate
For more information on module installation, please visit the detailed CPAN module installation guide.
|s||Focus search bar|
|?||Bring up this help dialog|
|g p||Go to pull requests|
|g i||go to github issues (only if github is preferred repository)|
|g a||Go to author|
|g c||Go to changes|
|g i||Go to issues|
|g d||Go to dist|
|g r||Go to repository/SCM|
|g s||Go to source|
|g b||Go to file browse|