Formats can be automatically detected from file extensions, they are used to find the right template and generate the correct Content-Type
header.
use Mojolicious::Lite;
# /detection
# /detection.html
# /detection.txt
get '/detection' => sub {
my $c = shift;
$c->render(template => 'detected');
};
app->start;
__DATA__
@@ detected.html.ep
<!DOCTYPE html>
<html>
<head><title>Detected</title></head>
<body>HTML was detected.</body>
</html>
@@ detected.txt.ep
TXT was detected.
The default format is html
, restrictive placeholders can be used to limit possible values.
use Mojolicious::Lite;
# /hello.json
# /hello.txt
get '/hello' => [format => [qw(json txt)]] => sub {
my $c = shift;
return $c->render(json => {hello => 'world'})
if $c->stash('format') eq 'json';
$c->render(text => 'hello world');
};
app->start;
Or you can just disable format detection with "without_formats" in Mojolicious::Routes::Route, and then re-enable it selectively on demand.
use Mojolicious::Lite;
app->routes->without_formats;
# /foo
get '/foo' => {text => 'No format detection again.'};
# /bar.txt
get '/bar' => [format => 'txt'] => {text => ' Just one format.'};
app->start;