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.


Dave


In reply to Re^3: Mojolicious, layouts and content generating. by davido
in thread Mojolicious, layouts and content generating. by Kyshtynbai

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.