Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-03-28 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found