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

Hi Monks!

I have a simple program that checks for user activities in an application.
I am starting to use Mojo Lite, it seems to be really good.
I am using my script by calling it from the browser as: http://www.myplace.com/mycode.pl/
It loads the main template, shows the initial page no problem. What I can not understand is why the
value on this line $c->stash( title => ' This is the text for the title' ); does not show
on the template, neither does this one $c->stash( result => ' This is the result for  testing' );
in the "/get_status" route.
I am also trying to stop these debug messages:
[debug] GET "/" [debug] Routing to a callback [debug] 200 OK (0.007915s, 126.342/s) [debug] GET "/get_status"
to be logged every time I call the script from
the browser. New to this and appreciate any help!

Here is a sample of the code I have:
#!/usr/bin/perl use Mojolicious::Lite; plugin 'HTMLTemplateProRenderer'; use Mojo::JSON qw(decode_json encode_json); use HTML::Template; use Data::Dump 'pp'; # dbh attribute app->attr(dbh => sub { my $c = shift; # I set a connection to the DB my $dbh = ...; # Passing connections in a hashref my $pass_dbh = {'dbh' => $dbh }; return $pass_dbh; }); any '/' => sub { my $c = shift; # Load Main Template $c->render( template => 'main', ); # Cant get this to show on the main template. $c->stash( title => ' This is the text for the title' ); }; get '/get_status' => sub { my $c = shift; my $get_update = $c->req->query_params->param('update'); # SQL goes here - this code is only for showing my logic my $data = (<<SQL); SELECT ... SQL # Load value into Main template, # but I cant get the results to display on the template. $c->render( template => 'main', ); # this is the tag in the template <TMPL_VAR NAME="result"> $c->stash( result => ' This is the result for testing' ); # I also return json from the sql query results, called from ajax # and it works fine. return $c->render(json => $data ); }; app->start;
Thanks for helping!

Replies are listed 'Best First'.
Re: Loading values into template using Perl and Mojolicious::Lite
by Corion (Patriarch) on Mar 31, 2017 at 18:11 UTC

    From looking at Mojolicious::Guides::Tutorial, it seems that you should ->stash before you ->render. Also, you should be able to pass your parameters directly in your call to ->render:

    $c->render(template => 'magic', two => 24);
      Yes, you're right the first one works, but the second does not, I wonder if it has to do with the " return $->render(json => $data ); code.

        You call ->render twice there. That doesn't make sense to me.