Last active
December 7, 2017 17:09
-
-
Save jberger/de56b339ebfbae7e8983ec2b573271a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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