Navigation Menu

Skip to content

Commit

Permalink
moved around a few sections in the guides
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 11, 2014
1 parent 737b3bb commit e51f30f
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 90 deletions.
5 changes: 4 additions & 1 deletion lib/Mojolicious/Guides/Cookbook.pod
Expand Up @@ -1277,7 +1277,7 @@ 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>, nested helpers with a prefix based on the plugin name
L<Mojolicious::Plugin>. Nested helpers with a prefix based on the plugin name
are an easy way to avoid conflicts.

package MyApp::Plugin::MyHelpers;
Expand Down Expand Up @@ -1311,6 +1311,9 @@ name.

app->start;

Of course these plugins can contain more than just helpers, take a look at
L<Mojolicious::Plugins/"PLUGINS"> for a few ideas.

=head2 Adding commands to Mojolicious

By now you've probably used many of the built-in commands described in
Expand Down
176 changes: 88 additions & 88 deletions lib/Mojolicious/Guides/Rendering.pod
Expand Up @@ -640,6 +640,92 @@ A naive translation to Perl code could look like this.
}
return $output;

=head2 Adding helpers

You should always try to keep your actions small and reuse as much code as
possible. Helpers make this very easy, you can use them to do pretty much
anything an action could do.

use Mojolicious::Lite;

helper debug => sub {
my ($c, $str) = @_;
$c->app->log->debug($str);
};

get '/' => sub {
my $c = shift;
$c->debug('Hello from an action!');
} => 'index';

app->start;
__DATA__

@@ index.html.ep
% debug 'Hello from a template!';

Helpers can also accept template blocks as last argument, this for example
allows very pleasant to use tag helpers and filters. Wrapping the helper
result into a L<Mojo::ByteStream> object can prevent accidental double
escaping.

use Mojolicious::Lite;
use Mojo::ByteStream;

helper trim_newline => sub {
my ($c, $block) = @_;
my $result = $block->();
$result =~ s/\n//g;
return Mojo::ByteStream->new($result);
};

get '/' => 'index';

app->start;
__DATA__

@@ index.html.ep
%= trim_newline begin
Some text.
%= 1 + 1
More text.
% end

Similar to stash values you can use a prefix like C<myapp.*> to keep helpers
from getting exposed in templates and to organize them into namespaces as your
application grows. Every prefix automatically becomes a helper that returns a
proxy object containing the current controller object and on which you can
call the nested helpers.

use Mojolicious::Lite;

helper 'cache_control.no_caching' => sub {
my $c = shift;
$c->res->headers->cache_control('private, max-age=0, no-cache');
};

helper 'cache_control.five_minutes' => sub {
my $c = shift;
$c->res->headers->cache_control('public, max-age=300');
};

get '/news' => sub {
my $c = shift;
$c->cache_control->no_caching;
$c->render(text => 'Always up to date.');
};

get '/some_older_story' => sub {
my $c = shift;
$c->cache_control->five_minutes;
$c->render(text => 'This one can be cached for a bit.');
};

app->start;

While helpers can also be redefined, this should only be done very carefully
to avoid conflicts.

=head2 Content blocks

Blocks and the helper L<Mojolicious::Plugin::DefaultHelpers/"content_for">
Expand Down Expand Up @@ -874,91 +960,9 @@ with L<Mojolicious::Validator::Validation/"csrf_protect">.

The token can also be submitted with the C<X-CSRF-Token> request header.

=head2 Adding helpers

You should always try to keep your actions small and reuse as much code as
possible. Helpers make this very easy, you can use them to do pretty much
anything an action could do.

use Mojolicious::Lite;

helper debug => sub {
my ($c, $str) = @_;
$c->app->log->debug($str);
};

get '/' => sub {
my $c = shift;
$c->debug('Hello from an action!');
} => 'index';

app->start;
__DATA__

@@ index.html.ep
% debug 'Hello from a template!';

Helpers can also accept template blocks as last argument, this for example
allows very pleasant to use tag helpers and filters. Wrapping the helper
result into a L<Mojo::ByteStream> object can prevent accidental double
escaping.

use Mojolicious::Lite;
use Mojo::ByteStream;

helper trim_newline => sub {
my ($c, $block) = @_;
my $result = $block->();
$result =~ s/\n//g;
return Mojo::ByteStream->new($result);
};

get '/' => 'index';

app->start;
__DATA__

@@ index.html.ep
%= trim_newline begin
Some text.
%= 1 + 1
More text.
% end

Similar to stash values you can use a prefix like C<myapp.*> to keep helpers
from getting exposed in templates and to organize them into namespaces as your
application grows. Every prefix automatically becomes a helper that returns a
proxy object containing the current controller object and on which you can
call the nested helpers.

use Mojolicious::Lite;

helper 'cache_control.no_caching' => sub {
my $c = shift;
$c->res->headers->cache_control('private, max-age=0, no-cache');
};

helper 'cache_control.five_minutes' => sub {
my $c = shift;
$c->res->headers->cache_control('public, max-age=300');
};

get '/news' => sub {
my $c = shift;
$c->cache_control->no_caching;
$c->render(text => 'Always up to date.');
};

get '/some_older_story' => sub {
my $c = shift;
$c->cache_control->five_minutes;
$c->render(text => 'This one can be cached for a bit.');
};

app->start;
=head1 ADVANCED

While helpers can also be redefined, this should only be done very carefully
to avoid conflicts.
Less commonly used and more powerful features.

=head2 Helper plugins

Expand Down Expand Up @@ -1086,10 +1090,6 @@ plugin.
@@ alertassets.html.ep
%= javascript "/alertassets.js"

=head1 ADVANCED

Less commonly used and more powerful features.

=head2 Rendering static files

If automatic rendering of static files is not enough, you can also render them
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Plugin/DefaultHelpers.pm
Expand Up @@ -162,7 +162,7 @@ Mojolicious::Plugin::DefaultHelpers - Default helpers plugin
=head1 DESCRIPTION
L<Mojolicious::Plugin::DefaultHelpers> is a collection of renderer helpers for
L<Mojolicious::Plugin::DefaultHelpers> is a collection of helpers for
L<Mojolicious>.
This is a core plugin, that means it is always enabled and its code a good
Expand Down

0 comments on commit e51f30f

Please sign in to comment.