Skip to content

Instantly share code, notes, and snippets.

/old.pod Secret

Created February 25, 2015 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/26443a7034b971926400 to your computer and use it in GitHub Desktop.
Save anonymous/26443a7034b971926400 to your computer and use it in GitHub Desktop.

Formats

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.

use Mojolicious::Lite;

# /hello
get '/hello' => [format => 0] => {text => 'No format detection.'};

# Disable detection and allow the following routes selective re-enabling
under [format => 0];

# /foo
get '/foo' => {text => 'No format detection again.'};

# /bar.txt
get '/bar' => [format => 'txt'] => {text => ' Just one format.'};

app->start;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment