Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added experimental support for helper methods

  • Loading branch information...
commit 5ca4d3d61fff693da48662485e96ce5297033f62 1 parent 41a3f81
Sebastian Riedel authored
View
1  Changes
@@ -9,6 +9,7 @@ This file documents the revision history for Perl extension Mojolicious.
Mojolicious::Guides::Cookbook recipes for more information.
- Added EXPERIMENTAL support for --mode and --home options to all
Mojolicious commands.
+ - Added EXPERIMENTAL support for helper methods.
- Added EXPERIMENTAL cache helper to
Mojolicious::Plugin::DefaultHelpers.
- Added EXPERIMENTAL write, write_chunk and rendered methods to
View
49 lib/Mojolicious/Controller.pm
@@ -10,6 +10,7 @@ use Mojo::Exception;
use Mojo::URL;
require Carp;
+require Scalar::Util;
# Space: It seems to go on and on forever...
# but then you get to the end and a gorilla starts throwing barrels at you.
@@ -36,15 +37,22 @@ sub finished {
sub helper {
my $self = shift;
- # Name
- return unless my $name = shift;
+ # Helper proxy
+ unless (exists $self->{_helpers}) {
- # Helper
- Carp::croak(qq/Helper "$name" not found/)
- unless my $helper = $self->app->renderer->helper->{$name};
+ # Helpers
+ $self->{_helpers} =
+ Mojolicious::Controller::_Helpers->new(_controller => $self);
+
+ # Weaken
+ Scalar::Util::weaken $self->{_helpers}->{_controller};
+ }
+
+ # Name
+ return $self->{_helpers} unless my $name = shift;
# Run
- return $self->$helper(@_);
+ return $self->{_helpers}->$name(@_);
}
sub receive_message {
@@ -415,6 +423,32 @@ sub write_chunk {
$self->rendered;
}
+# Helper proxy
+package Mojolicious::Controller::_Helpers;
+
+use base 'Mojo::Base';
+
+our $AUTOLOAD;
+
+sub AUTOLOAD {
+ my $self = shift;
+
+ # Method
+ return unless my $method = (split '::' => $AUTOLOAD)[-1];
+
+ # Controller
+ return unless my $c = $self->{_controller};
+
+ # Helper
+ Carp::croak(qq/Helper "$method" not found/)
+ unless my $helper = $c->app->renderer->helper->{$method};
+
+ # Run
+ return $c->$helper(@_);
+}
+
+sub DESTROY { }
+
1;
__END__
@@ -485,12 +519,15 @@ Callback signaling that the transaction has been finished.
=head2 C<helper>
+ $c->helper->foo;
+ $c->helper->foo(23);
$c->helper('foo');
$c->helper(foo => 23);
Directly call a L<Mojolicious> helper, see
L<Mojolicious::Plugin::DefaultHelpers> for a list of helpers that are always
available.
+Note that this method is EXPERIMENTAL and might change without warning!
=head2 C<receive_message>
View
2  lib/Mojolicious/Guides/Rendering.pod
@@ -229,7 +229,7 @@ controller method to call them.
<%= dumper [1, 2, 3] %>
- my $serialized = $self->helper(dumper => [1, 2, 3]);
+ my $serialized = $self->helper->dumper([1, 2, 3]);
The C<dumper> helper will use L<Data::Dumper> to serialize whatever data
structure you pass it, this can be very useful for debugging.
View
2  t/mojolicious/lite_app.t
@@ -1410,7 +1410,7 @@ Just works!\
@@ autostash.html.ep
% $self->helper(layout => 'layout');
%= $foo
-%= param 'bar'
+%= $self->helper->param('bar')
% my $foo = 42;
%= $foo
%= $self->match->endpoint->name;
Please sign in to comment.
Something went wrong with that request. Please try again.