Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Mojolicious, layouts and content generating.

by Kyshtynbai (Sexton)
on Jun 10, 2014 at 05:08 UTC ( #1089381=perlquestion: print w/replies, xml ) Need Help??

Kyshtynbai has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks! I'm new to Mojolicious and I've got the following question. I'd like to have a drop-down menu which would be generated from a list of directories in a specific directories. Here's a helper for getting this list:
helper thumbs => sub { my @thumbs; my $dir = 'public/images/thumbs'; my $dh; opendir $dh, $dir; foreach (readdir $dh) { next if /^\.$|^\.\.$/; #let's assume there are no files in thi +s dir, only dirs. push @thumbs, $_; } closedir $dh; return @thumbs; };
I also have a main.html.ep which is a layout for all the other "pages". Here it is:
<html> <head> <link rel="stylesheet" href="../main.css"> <body> %= include 'header' <div id="main_div"> <div id="menu"> <ul> <li>link1</li> <li>link2</li> <li>link3</li> </ul> </div> <div id="r_menu"> <div class="inner_block_1"> some block </div> <div id="content"> <%= content %> </div> </div> <div id="footer"> <p> Footer info. </p> </div> </body> </html>
As you can see, this layout includes "header.html.ep" template, and it is where drop-down menu is generated. The quesion is: how do I pass the arrayref of @thumbs array to the header.html.ep template? This header.html.ep is stored in "templates" directory and it is not even mentioned in! In case you need it, I provide the code for header.html.ep, but it is not very important in this case:
<div id="header"> <div id="top_menu"> <ul id="menul"> <li><a href="/index">Root</a></li> <li><a href="/quotes">Quotes</a></li> <li id="sub1">Pics <ul> <% some perl code to generate <li> tags <li><a href="vu">pic</a></li> </ul> </li> <li><a href = "/logout">&#1042;&#1099;&#1081;&#1090;&#1080;</a></l +i> </ul> </div> </div>
Thank you in advance!

Replies are listed 'Best First'.
Re: Mojolicious, layouts and content generating.
by neilwatson (Priest) on Jun 10, 2014 at 11:31 UTC
      It didn't come to my mind, gotta check it out, thanks!

        The Mojolicious::Lite documentation is where you should be starting as you learn to use Mojolicious. "stash" may not have been an obvious solution if one is trying to figure it out without the benefit of documentation, but it is a fundamental Mojolicious concept, well documented, and exists specifically for this purpose: Stash and Templates:

        The "stash" in Mojolicious::Controller is used to pass data to templates,...

        use Mojolicious::Lite; # Route leading to an action that renders a template get '/bar' => sub { my $self = shift; $self->stash(one => 23); $self->render('baz', two => 24); }; app->start; __DATA__ @@ baz.html.ep The magic numbers are <%= $one %> and <%= $two %>.

        From that documentation and example we can see at least two ways to pass information into templates. The first is the explicit call to stash, and the second is passing a stash value through the render call.

        From within the template, stash values become Perl variables. This is useful, unless your controller logic creates certain stash values only under certain conditions, in which case the variable may not exist within a template, and a stricture violation will result. For example:

        get '/bar' => sub { my $self = shift; if( $cond ) { $self->stash( one => 23 ); } else { $self->stash( two => 42 ); } $self->render('baz'); } app_start; __DATA__ @@ baz.html.ep <%= $one %> or <%= $two %>.

        Only one of those two stash values will be populated, and only one of the two will exist as variables. The simple solution is to ensure that you never create logic that will result in this shortcoming. But sometimes that can be a cumbersome limitation. Another solution is to call stash() from within the template, rather than relying on the existence of a variable in the template. This is documented in Mojolicious::Plugin::DefaultHelpers "stash":

        %= stash 'foo' % stash foo => 'bar'; ... %= stash('name') // 'Somebody'

        So here within the template we're calling "stash" to retrieve a specific stash value, or in the case of stash foo => 'bar';, to set a value. In this way we're avoiding the issue of different logic paths creating different stash elements. In the final example, "%= stash('name') // 'Somebody'", if "name" is undefined, the "Somebody" value will be used.


Re: Mojolicious, layouts and content generating.
by Anonymous Monk on Jun 10, 2014 at 07:13 UTC
      #!/usr/bin/perl -- # Automatically use-s strict/warnings/utf8 and feature :5.10 use Mojolicious::Lite; sub mojo_index { my( $self ) = @_; my $time = time; my @three = ( 1..3 ); return $self->render( title => "Mojo $time", thumbs => \@three ); } ## map paths to subroutines ### #~ route named 'index', render's template index.html.ep if html is acc +epted get '/' => \&mojo_index => 'index';;; #~ https://localhost:3000/index #~ https://localhost:3000/index.html #~ https://localhost:3000/index.txt get '/index' => sub { shift->render( title => "Mojo \$time" , thumbs = +> [ 6,6,6] ) }; helper thumbs => sub { [ 9,9,9 ] }; helper thumbstring => sub { join ' ', @{[ 9,9,9 ]} }; # Start the Mojolicious command system _aka_ shagadelic('daemon'); app->start; #~ +e __DATA__ @@ thumbs.html.ep <b> I am thumbs.html.ep </b> <dl> <dt> $thumbs <dd> <%= $thumbs =%> <dt> @$thumbs <dd> <%= join ' ', @$thumbs =%> <dt> thumbs() <dd> <%= join ' ', thumbs(); =%> <dt> @{thumbs()} <dd> <%= join ' ', @{thumbs()}; =%> <dt> thumbstring() <dd> <%= thumbstring =%> </dl> @@ index.txt.ep If you ask for text you will get text its <%= title %> @@ index.html.ep <html> <head> <title><%= title =%></title> <meta charset="utf-8" /></meta> </head> <body> <h1><%= title =%></h1> <dl> <dt> $thumbs <dd> <%= $thumbs =%> <dt> @$thumbs <dd> <%= join ' ', @$thumbs =%> <dt> thumbs() <dd> <%= join ' ', thumbs(); =%> <dt> @{thumbs()} <dd> <%= join ' ', @{thumbs()}; =%> <dt> thumbstring() <dd> <%= thumbstring =%> </dl> <hr> <div> <%= include 'thumbs' =%> </div> </body> </html>

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1089381]
Approved by vinoth.ree
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2023-12-08 19:24 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (37 votes). Check out past polls.