Skip to content

Instantly share code, notes, and snippets.

@kraih
Created June 25, 2011 12:52
Show Gist options
  • Save kraih/1046452 to your computer and use it in GitHub Desktop.
Save kraih/1046452 to your computer and use it in GitHub Desktop.
use Mojolicious::Lite;
BEGIN { plugin 'Evil' }
get '/' => sub {
render text => 'Hello World!';
};
app->start;
package Evil;
use Mojo::Base 'Mojolicious::Plugin';
# Singleton controller
our $CONTROLLER;
sub register {
my ($self, $app) = @_;
# Localize singleton controller
$app->on_process(
sub {
my ($self, $c) = @_;
local $CONTROLLER = $c;
$self->dispatch($c);
}
);
# "render" helper
$app->helper(render => sub { shift->render(@_) });
# Export helpers
no strict 'refs';
my $caller = caller(3);
for my $helper (keys %{$app->renderer->helpers}) {
*{"${caller}::$helper"} = sub { $CONTROLLER->$helper(@_) }
unless defined *{"${caller}::$helper"};
}
}
1;
use Mojolicious::Lite;
BEGIN { plugin 'Evil' }
get '/' => sub {
layout 'green';
title 'Welcome!';
render 'login', message => 'Hi there!';
};
app->start;
__DATA__
@@ layouts/green.html.ep
<!DOCTYPE html><html>
<head><title><%= title %></title></head>
<body><%= content %></body>
</html>
@@ login.html.ep
Message: <%= $message %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment