#!/usr/bin/env perl use Mojolicious::Lite -signatures; any '/' => sub ($c) { $c->redirect_to('/myform.html') }; sub my_api_do_thing ($arg) { # could/should be in a module! return $arg =~ s/[aeiou]//gr; } any '/myform' => sub ($c) { if ( defined $c->param('foo') ) { # if the form was submitted $c->stash( returnval => my_api_do_thing( $c->param('foo') ) ); } $c->respond_to( json => { json => { retval => $c->stash('returnval') } }, txt => { text => $c->stash('returnval') }, html => { template => 'myform' }, ); }; app->start; __DATA__ @@ myform.html.ep % layout 'main', title => 'Hello, World!';
% if ( stash 'returnval' ) {
Return value: <%= stash 'returnval' %>
% } %= form_for myform => ( method=>'post' ) => begin
%= label_for foo => 'Foo' %= text_field foo=>'Foobar?'
%= radio_button format => 'html', id=>'format_html', checked => undef %= label_for format_html => 'HTML' %= radio_button format => 'json', id=>'format_json' %= label_for format_json => 'JSON'
%= submit_button
%= end
@@ layouts/main.html.ep <%= title %> %= content