A musty reference to a bookmark variable is not always defined

I am still studying mosaic and MVC structures in general, so this can be a problem when I think about it wrong, so if I offer you the best way to do the following.

I have route / route /: param where the parameter is sometimes defined and sometimes not. I try to use "param" in the template for this route, but I get the error message "param" requires an explicit package name. I know that this is due to the fact that: param does not match the route, because when I call / trace / evaluate, everything works fine.

Is there a way to use the same pattern for both when "param" is defined and not defined? I am just trying to pre-fill a form from what is defined in "param", but does not require it.

In the template I have

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

Thanks.

+7
source share
3 answers

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

 <%= stash "param" %> <%= defined(stash("param")) && stash("param") %> etc. 
+14
source

You can define stash (or flash ) as a Perl variable in an epl space / template so that you can reuse it if necessary. eg.

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

In this case, the if block will be displayed only when the parameter is defined in stash , otherwise it will be skipped.

+1
source

It seems that in this situation, using an optional placeholder on the route might be the best option. If the placeholder is defined in the route itself, this definition will be used if the placeholder is not specified in the URL (otherwise, the value specified in the URL is used).

For example:

 $r->any('/page/:paramVar')->to('page#doTheThing', paramVar => 'cake'); 

If the address "/ page" is loaded, then $self->param('paramVar') == 'cake' else, if "/ page / tree" is loaded, then $self->param('paramVar') == 'tree' .

Note. As with other placeholder values, an additional placeholder, such as paramVar, used in the above example, can be accessed through the stash function, as well as the param function: $self->stash('paramVar') .

+1
source

All Articles