Created
December 16, 2010 10:29
-
-
Save yko/743265 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
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
get '/' => 'index'; | |
get '/set-admin' => sub { | |
my $self = shift; | |
$self->session->{this_user_is_admin} = 1; | |
$self->redirect_to('/'); | |
} => 'became admin'; | |
get '/unset-admin' => sub { | |
my $self = shift; | |
$self->session->{this_user_is_admin} = 0; | |
$self->redirect_to('/'); | |
} => 'logout'; | |
app->helper( | |
admin => sub { | |
my $c = shift; | |
my $cb = shift; | |
# By default return 1 on success | |
$cb ||= sub {1}; | |
if ($c->session->{this_user_is_admin} == 1) { | |
return Mojo::ByteStream->new($cb->(@_)); | |
} | |
return undef; | |
} | |
); | |
app->start; | |
__DATA__ | |
@@ index.html.ep | |
% layout 'default'; | |
<p>Welcome to Mojolicious!</p> | |
% # You can use 'admin' helper as boolean | |
% if (!admin) { | |
You can <%= link_to 'became admin' => begin %>obtain adminship<%end%> (only today!) | |
% } | |
% # And format pretty block of code with new begin/end block! | |
<%= admin begin %> | |
You are admin, but you can <%= link_to logout => begin %>logout<%end%> anytime. | |
<% end %> | |
@@ layouts/default.html.ep | |
<!doctype html><html> | |
<head><title>Welcome!</title></head> | |
<body> | |
<%== content %> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
a