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>

In reply to Re: changing title of a webpage dynamically ( Mojolicious::Lite Time::Piece ) by Anonymous Monk
in thread changing title of a webpage dynamically by tejas

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.