Using Mojolicious::Lite and its templating system
#!/usr/bin/perl --
use strict;
use warnings;
use Time::Piece;
use Time::Seconds();
use Mojolicious::Lite; ## flips on strict/warnings
sub date_time_mojo {
my $self = shift;
my $dir = $self->param('dir');
my $today = $self->param('today');
my $thismonth = $self->param('thismonth');
my $thisyear = $self->param('thisyear');
my $time ;
if( $dir and $today and $thismonth ){
$time = Time::Piece->strptime("$today $thismonth $thisyear", '
+%d %m %Y');
if( lc $dir eq 'yesterday'){
$time -= Time::Seconds::ONE_DAY() ;
} else {
$time += Time::Seconds::ONE_DAY() ;
}
} else {
$time = gmtime;
}
$self->render(
t => $time,
title => $time->ymd,
);
}
get '/' => \&date_time_mojo => 'index';;
app->start;
__DATA__
@@ index.html.ep
% layout 'green';
%= content content => begin
<%== $t->ymd %>
<form name="input" method="GET">
<input type="hidden" name="today" value="<%== $t->mday %>">
<input type="hidden" name="thismonth" value="<%== $t->mon %>">
<input type="hidden" name="thisyear" value="<%== $t->year %>">
<input type="submit" name="dir" value="Yesterday"></td>
<input type="submit" name="dir" value="Tomorrow"></td>
</form>
% end
@@ layouts/green.html.ep
<!DOCTYPE html>
<html>
<head><title><%== title %></title></head>
<body bgcolor='lightgreen'><%= content %></body>
</html>
Here is how you can test this from cli for today
$ perl fudge get /
[Fri Jun 7 01:31:06 2013] [debug] Your secret passphrase needs to be
+changed!!!
[Fri Jun 7 01:31:06 2013] [debug] GET / (Mojolicious (Perl)).
[Fri Jun 7 01:31:06 2013] [debug] Routing to a callback.
[Fri Jun 7 01:31:06 2013] [debug] Rendering template "index.html.ep"
+from DATA section.
[Fri Jun 7 01:31:06 2013] [debug] Rendering template "layouts/green.h
+tml.ep" from DATA section.
[Fri Jun 7 01:31:06 2013] [debug] 200 OK (0.055773s, 17.930/s).
<!DOCTYPE html>
<html>
<head><title>2013-06-07</title></head>
<body bgcolor='lightgreen'>
2013-06-07
<form name="input" method="GET">
<input type="hidden" name="today" value="7">
<input type="hidden" name="thismonth" value="6">
<input type="hidden" name="thisyear" value="2013">
<input type="submit" name="dir" value="Yesterday"></td>
<input type="submit" name="dir" value="Tomorrow"></td>
</form>
</body>
</html>
Here is how you can test this from cli for tomorrow
$ perl fudge get /?today=7;thismonth=6;thisyear=2013;dir=Tomorrow
[Fri Jun 7 01:32:20 2013] [debug] Your secret passphrase needs to be
+changed!!!
[Fri Jun 7 01:32:20 2013] [debug] GET / (Mojolicious (Perl)).
[Fri Jun 7 01:32:20 2013] [debug] Routing to a callback.
[Fri Jun 7 01:32:20 2013] [debug] Rendering template "index.html.ep"
+from DATA section.
[Fri Jun 7 01:32:20 2013] [debug] Rendering template "layouts/green.h
+tml.ep" from DATA section.
[Fri Jun 7 01:32:20 2013] [debug] 200 OK (0.058046s, 17.228/s).
<!DOCTYPE html>
<html>
<head><title>2013-06-08</title></head>
<body bgcolor='lightgreen'>
2013-06-08
<form name="input" method="GET">
<input type="hidden" name="today" value="8">
<input type="hidden" name="thismonth" value="6">
<input type="hidden" name="thisyear" value="2013">
<input type="submit" name="dir" value="Yesterday"></td>
<input type="submit" name="dir" value="Tomorrow"></td>
</form>
</body>
</html>