#!/usr/bin/env perl use Mojolicious::Lite; use Mojo::SQLite; helper sqlite => sub { state $sql = Mojo::SQLite->new('sqlite:mydb.db') }; # disable template cache when running under morbo app->renderer->cache->max_keys(0) if app->mode eq 'development'; app->sqlite->migrations->from_string(<<'END_MIGR')->migrate; -- 1 up CREATE TABLE contacts ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, department TEXT ); INSERT INTO contacts (name,department) VALUES ('John Doe','Perl'); -- 1 down DROP TABLE IF EXISTS contacts; END_MIGR get '/' => sub { my $c = shift; $c->render('main'); } => 'index'; post '/search' => sub { my $c = shift; $c->redirect_to( $c->url_for('name_query', query => $c->param('name') ) ); } => 'search'; get '/name/:query' => sub { my $c = shift; $c->stash( rows => $c->sqlite->db->select( 'contacts', ['id','name','department'], { name => $c->stash('query') } )->arrays ); $c->render('main'); } => 'name_query'; app->start; __DATA__ @@ main.html.ep Hello Mojo!
%= form_for search => ( method => 'post' ) => begin %= label_for name => 'Name' %= text_field 'name', required=>'required'; %= submit_button 'Query' %= end
% if ( my $rows = stash 'rows' ) { % if (@$rows) {
% foreach my $row (@$rows) { % foreach my $text (@$row) { % } % }
id name department
<%= $text %>
% } else {
No records found!
% } % }