Skip to content

Commit

Permalink
use more nested helpers in recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 3, 2014
1 parent 8747ad9 commit 9faf4ff
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -141,7 +141,7 @@ sub helper {
my ($self, $name, $cb) = @_;
my $r = $self->renderer;
$self->log->debug(qq{Helper "$name" already exists, replacing.})
if $r->get_helper($name);
if exists $r->helpers->{$name};
$r->add_helper($name => $cb);
}

Expand Down
9 changes: 5 additions & 4 deletions lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -1242,16 +1242,17 @@ application, you can use application specific plugins.
$ touch lib/MyApp/Plugin/MyHelpers.pm

They work just like normal plugins and are also subclasses of
L<Mojolicious::Plugin>.
L<Mojolicious::Plugin>, nested helpers with a prefix based on the plugin name
are an easy way to avoid conflicts.

package MyApp::Plugin::MyHelpers;
use Mojo::Base 'Mojolicious::Plugin';

sub register {
my ($self, $app) = @_;
$app->helper(render_with_header => sub {
$app->helper('my_helpers.render_with_header' => sub {
my ($c, @args) = @_;
$c->res->headers->header('X-Mojo' => 'I <3 Mojolicious!')
$c->res->headers->header('X-Mojo' => 'I <3 Mojolicious!');
$c->render(@args);
});
}
Expand All @@ -1270,7 +1271,7 @@ name.

get '/' => sub {
my $c = shift;
$c->render_with_header(text => 'I ♥ Mojolicious!');
$c->my_helpers->render_with_header(text => 'I ♥ Mojolicious!');
};

app->start;
Expand Down

0 comments on commit 9faf4ff

Please sign in to comment.