| 1 | package Template::Plugin::DateTime::Format; | ||||||||||||
| 2 | BEGIN { | ||||||||||||
| 3 | 3 | ▶ | $Template::Plugin::DateTime::Format::VERSION = '0.02'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | } | ||||||||||||
| 5 | # ABSTRACT: format DateTime objects from inside TT with C<DateTime::Format>-style formatters | ||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 3 | ▶ | use DateTime; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 3 | ▶ | use Class::MOP; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | |||||||||||||
| 11 | 3 | ▶ | use base 'Template::Plugin'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | sub new { | ||||||||||||
| 14 | 3 | ▶ | my ($class, $context, $formatter_class, $new_args, $format_args) = @_; | ||||||||||
|
Subroutine
new: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||
| 15 | 3 | ▶ | Class::MOP::load_class($formatter_class || die 'need class name'); | ||||||||||
|
Condition100%2/2
$formatter_class || die('need class name')
MC/DC100%1/1
$formatter_class || die('need class name')
$formatter_class
Truth table100%2/2
A
A= $formatter_class
| |||||||||||||
| 16 | |||||||||||||
| 17 | 1 | ▶ | my @new_args = ref $new_args eq 'ARRAY' ? @$new_args : $new_args; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 18 | 3 | ▶ | { no warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | 1 | ▶ | $format_args = [ $format_args ] unless ref $format_args eq 'ARRAY'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | } | ||||||||||||
| 21 | |||||||||||||
| 22 | bless { | ||||||||||||
| 23 | 1 | _CONTEXT => $context, | |||||||||||
| 24 | formatter => $formatter_class->new(@new_args), | ||||||||||||
| 25 | format_args => $format_args, | ||||||||||||
| 26 | }, $class; | ||||||||||||
| 27 | } | ||||||||||||
| 28 | |||||||||||||
| 29 | sub format { | ||||||||||||
| 30 | 1 | ▶ | my ($self, $date) = @_; | ||||||||||
|
Subroutine
format: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 31 | |||||||||||||
| 32 | 1 | my $fmt = $self->{formatter}; | |||||||||||
| 33 | 1 | my @args = $self->{format_args}; | |||||||||||
| 34 | 1 | return $fmt->format_datetime($date, @args); | |||||||||||
| 35 | } | ||||||||||||
| 36 | |||||||||||||
| 37 | 3 | 1; | |||||||||||
| 38 | |||||||||||||
| 39 | |||||||||||||
| 40 | |||||||||||||
| 41 | =pod | ||||||||||||
| 42 | |||||||||||||
| 43 | =head1 NAME | ||||||||||||
| 44 | |||||||||||||
| 45 | Template::Plugin::DateTime::Format - format DateTime objects from inside TT with C<DateTime::Format>-style formatters | ||||||||||||
| 46 | |||||||||||||
| 47 | =head1 VERSION | ||||||||||||
| 48 | |||||||||||||
| 49 | version 0.02 | ||||||||||||
| 50 | |||||||||||||
| 51 | =head1 SYNOPSIS | ||||||||||||
| 52 | |||||||||||||
| 53 | [% USE f = DateTime::Format('DateTime::Format::Strptime', { pattern => "%T" }) %] | ||||||||||||
| 54 | [% f.format(datetime_object) %] | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 DESCRIPTION | ||||||||||||
| 57 | |||||||||||||
| 58 | Oftentimes, you have a L<DateTime|DateTime> object that you want to | ||||||||||||
| 59 | render in your template. However, the default rendering | ||||||||||||
| 60 | (2008-01-01T01:23:45) is pretty ugly. Formatting the DateTime with a | ||||||||||||
| 61 | L<DateTime::Format|DateTime::Format> object is the usual solution, but | ||||||||||||
| 62 | there's usually not a nice place to put the formatting code. | ||||||||||||
| 63 | |||||||||||||
| 64 | This plugin solves that problem. You can create a formatter object | ||||||||||||
| 65 | from within TT and then use that object to format DateTime objects. | ||||||||||||
| 66 | |||||||||||||
| 67 | =head2 CREATING AN OBJECT | ||||||||||||
| 68 | |||||||||||||
| 69 | Creating a formatter instance is done in the usual TT way: | ||||||||||||
| 70 | |||||||||||||
| 71 | [% USE varname = DateTime::Format( ... args ... ) %] | ||||||||||||
| 72 | |||||||||||||
| 73 | This creates a new formatter and calls it C<varname>. | ||||||||||||
| 74 | |||||||||||||
| 75 | The constructor takes up to three arguments. The first argument is | ||||||||||||
| 76 | the name of the formatter class. It is required, and the named class | ||||||||||||
| 77 | must follow the DateTime::Format API. An exception will be thrown if | ||||||||||||
| 78 | the class cannot be loaded. | ||||||||||||
| 79 | |||||||||||||
| 80 | The second argument is a reference to pass | ||||||||||||
| 81 | to the formatter's constructor. If it is an array reference, the | ||||||||||||
| 82 | array will be dereferenced before being passed to C<new> as C<@_>. | ||||||||||||
| 83 | Otherwise, the single reference is passed to the constructor. | ||||||||||||
| 84 | |||||||||||||
| 85 | The third argument is optional and is the rest of C<@_> to pass to | ||||||||||||
| 86 | C<format_datetime> after the DateTime object. I don't know if this is | ||||||||||||
| 87 | actually allowed by the API, but I figured it might come in handy. | ||||||||||||
| 88 | |||||||||||||
| 89 | =head2 FORMATTING DATES | ||||||||||||
| 90 | |||||||||||||
| 91 | Once you've created the object, invoke the C<format> method with the | ||||||||||||
| 92 | DateTime object you'd like to format. The result of | ||||||||||||
| 93 | C<format_datetime> is returned. | ||||||||||||
| 94 | |||||||||||||
| 95 | =head1 METHODS | ||||||||||||
| 96 | |||||||||||||
| 97 | =head2 new | ||||||||||||
| 98 | |||||||||||||
| 99 | Called by TT to create a new formatter. | ||||||||||||
| 100 | |||||||||||||
| 101 | =head2 format($datetime) | ||||||||||||
| 102 | |||||||||||||
| 103 | Formats C<$datetime>. | ||||||||||||
| 104 | |||||||||||||
| 105 | =head1 AUTHOR | ||||||||||||
| 106 | |||||||||||||
| 107 | Jonathan Rockway C<< <jrockway@cpan.org> >> | ||||||||||||
| 108 | |||||||||||||
| 109 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 110 | |||||||||||||
| 111 | Copyright (c) 2008 Jonathan Rockway. | ||||||||||||
| 112 | |||||||||||||
| 113 | This module is free software. You may redistribute it under the same | ||||||||||||
| 114 | terms as perl itself. | ||||||||||||
| 115 | |||||||||||||
| 116 | =head1 AUTHOR | ||||||||||||
| 117 | |||||||||||||
| 118 | Jonathan Rockway <jrockway@cpan.org> | ||||||||||||
| 119 | |||||||||||||
| 120 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 121 | |||||||||||||
| 122 | This software is copyright (c) 2010 by Jonathan Rockway. | ||||||||||||
| 123 | |||||||||||||
| 124 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 125 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 126 | |||||||||||||
| 127 | =cut | ||||||||||||
| 128 | |||||||||||||
| 129 | |||||||||||||
| 130 | __END__ | ||||||||||||
| 131 | |||||||||||||