I need to pass the result of
Mojo::mysql
to $variable
in HTML
#!/usr/bin/env perl
use Mojolicious::Lite;
use Mojo::mysql;
helper mysql => sub {
state $pg = Mojo::mysql->new( 'mysql://mysql://sri:s3cret@localhost/db' )
};
get '/index' => sub {
my $c = shift;
my $db = $c->mysql->db;
$db->query( 'select now() as time' )->hash;
};
app->start;
__DATA__
@@ index.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= $db %></body>
</html>`
I need to pass the result of $db
to index, but I don't know how to this.
You never rendered your index
template. To do so, call the render
method on your controller. Any variables in your stash (or other arguments to the render function) are turned into variables in your template.
So we would have something like this:
get '/index' => sub {
my $c = shift;
...
my $result = $db->query(...)->hash;
return $c->render(template => 'index', result => $result);
};
...
__DATA__
@@ index.html.ep
<!DOCTYPE html>
<html>
<head><title><%= title %></title></head>
<body><%= dumper $result %></body>
</html>
(Here I've used the dumper
helper so that the hash ref produces useful output, not just HASH(0x123abc)
.)
Further reading: