Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mojolicious referencing a stash variable not always defined

I am still learning mojolicious and MVC frameworks in general so this just might be a problem where I am thinking about this wrong so if I am please suggest a better way to do the following.

I have a route /route/:param where param is sometimes defined and sometimes not. I am trying to use "param" in the template for that route but I get an error saying "param" requires explicit package name. I know this is due to :param not matching in the route because when I do call /route/value everything works fine.

Is there a way to be able to use the same template for both when "param" is defined and not defined? I am just trying to pre-populate a form off of what is defined in "param" but not making it required to.

In the template I have

<% if(defined($param)){ %><%= $param %><% } %>

Thanks.

like image 947
Justin Belfield Avatar asked Jun 05 '13 15:06

Justin Belfield


2 Answers

It is always safe to refer to stash("param"), where stash is a helper function defined in Mojolicious::Plugin::DefaultHelpers:

<%= stash "param" %>
<%= defined(stash("param")) && stash("param") %>
etc.
like image 147
mob Avatar answered Nov 16 '22 02:11

mob


It is possible to define a stash (or a flash) variable as a Perl variable within the epl space/template so that it can be reused, if required. e.g.,

% if (my $param = stash 'param') {                                  
    $param
% }

In this case the if condition block will be rendered only when the param is defined in the stash, otherwise, it'll be skipped.

like image 40
ashraf Avatar answered Nov 16 '22 00:11

ashraf