Skip to content

Instantly share code, notes, and snippets.

@jberger
Last active December 7, 2017 17: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 jberger/de56b339ebfbae7e8983ec2b573271a1 to your computer and use it in GitHub Desktop.
Save jberger/de56b339ebfbae7e8983ec2b573271a1 to your computer and use it in GitHub Desktop.
use Mojolicious::Lite;
# I don't think defaulting both dir and page works, so separate root handler is necessary below
get '/*dir/:page' => {dir => ''} => sub {
my $c = shift;
# attempt to render the requested page
my @parts = (split(m{/}, $c->stash('dir')), $c->stash('page'));
$c->stash(template => join '/', @parts);
return if $c->render_maybe;
# attempt to render _index pages in the containing directories
$parts[-1] = '_index';
while (1) {
$c->stash(template => join '/', @parts);
return if $c->render_maybe;
last if @parts < 2;
splice @parts, -2, 1;
}
# no _index page found
$c->render(status => 404, text => 'Not found');
};
get '/' => '_index';
app->start;
__DATA__
@@ some/path/this.html.ep
Hello from the page some/path/this.html
@@ some/path/_index.html.ep
Hello from some/path
@@ some/_index.html.ep
Hello from some/
@@ _index.html.ep
Hello from /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment